It looks like you're new here. If you want to get involved, click one of these buttons!
<?php/* * PHP code to export MySQL data to CSV * http://salman-w.blogspot.com/2009/07/export-mysql-data-to-csv-using-php.html * * Sends the result of a MySQL query as a CSV file for download *//* * establish database connection */ include('client.inc.php'); //make sure u add thisif(!defined('OSTCLIENTINC') || !is_object($thisclient) || !$thisclient->isValid()) die('Kwaheri'); //... and this$conn = mysql_connect('localhost', 'dbuser', 'dbpass') or die(mysql_error());mysql_select_db('dbname', $conn) or die(mysql_error($conn));/* * execute sql query I totally copy/past this part from the tickets.inc.php, i've added the tables i needed tho' :D */$qwhere =' WHERE email='.db_input($thisclient->getEmail()); // STRICT TO THE USER$qfrom=' FROM '.TICKET_TABLE.' ticket LEFT JOIN '.DEPT_TABLE.' dept ON ticket.dept_id=dept.dept_id ';//Pagenation stuff....wish MYSQL could auto pagenate (something better than limit)$result=mysql_query('SELECT ticket.ticketID , ville , subject,name,email,status,ticket.created,paiement,numcmdmar,nomcli,closed'.$qfrom.' '.$qwhere);//$result1 = mysql_query($qselect,$qfrom,$qwhere, $conn) or die(mysql_error($conn));/* * send response headers to the browser * following headers instruct the browser to treat the data as a csv file called export.csv */header('Content-Type: text/csv');header('Content-Disposition: attachment;filename=export.csv');/* * output header row (if atleast one row exists) */$row = mysql_fetch_assoc($result);if ($row) { echocsv(array_keys($row));}/* * output data rows (if atleast one row exists) */while ($row) { echocsv($row); $row = mysql_fetch_assoc($result);}/* * echo the input array as csv data maintaining consistency with most CSV implementations * - uses double-quotes as enclosure when necessary * - uses double double-quotes to escape double-quotes * - uses CRLF as a line separator */function echocsv($fields){ $separator = ''; foreach ($fields as $field) { if (preg_match('/\\r|\\n|,|\"/', $field)) { $field = '\"' . str_replace('\"', '\"\"', $field) . '\"'; } echo $separator . $field; $separator = ','; } echo \"\r\n\";}?>
Comments