The below working example will help us to understand this better. We are using a database having name "shaliwahan" and the table name "tablename".
<?php function cleanData(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; } $filename = "website_data_" . date('Ymd') . ".xls"; header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-Type: application/vnd.ms-excel"); $flag = false; $con=mysql_connect("servername","username","password"); $db=mysql_select_db("shaliwahan",$con); $result = mysql_query("SELECT * FROM tablename") or die('Query failed!'); while(false !== ($row = mysql_fetch_assoc($result))) { if(!$flag) { echo implode("\t", array_keys($row)) . "\n"; $flag = true; } array_walk($row, 'cleanData'); echo implode("\t", array_values($row)) . "\n"; } ?>
In the above code you can customize the file name in the file name variable @ "$filename". The "$con" is to be changed with the MySQL server details (hostname / username / password).
In the $db variable change the tablename with the name of your table. The rest can go unaltered.
I understand that you must be having some basic knowledge about PHP and can very well implement this code. But if you need help you can always comment back on the same post.
No comments:
Post a Comment
Comment anything you want. Just be polite and give respect to others!
I am simply going to remove the comments which are offensive or are off topic.
And please don't spam your website links in comments. I don't, neither should you.