abdfahim Posted May 12, 2008 Share Posted May 12, 2008 Is there any built in function to output a SQL query directly to a CSV/tab delimited file without writing line by line? Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/ Share on other sites More sharing options...
jaymc Posted May 12, 2008 Share Posted May 12, 2008 I seriously doubt it, but, others will have created classes that will do this, maybe use them? Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-538934 Share on other sites More sharing options...
Xurion Posted May 12, 2008 Share Posted May 12, 2008 Use this. <?php //head information for a csv file header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); Header("content-type: text/html"); header("Content-Disposition: attachment; filename=\"website-data.csv\";"); header("Content-Transfer-Encoding: binary"); //echo the first line to be the titles of the columns (notice the return at the end of the line) echo '"product ID","ref","title","description","price" '; //query the db for the information $result = mysql_query("SELECT * FROM products"); //echo each record (notice the return at the end of the line) while ($row = mysql_fetch_array($result)){ echo '"'.$row[product_id].'","'.$row[ref].'","'.$row[title].'","'.$row[description].'","'.$row[price].'" '; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-538976 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2008 Share Posted May 12, 2008 Why go through the effort of writing out headers, you can just as easily do something like this: <?php $fh = fopen('test.csv','w'); $flds = array("product ID","ref","title","description","price"); fputcsv($fh,$flds); $q = "select product_id,ref,title,description from yourtable"; $rs = mysql_query($q); while ($rw = mysql_fetch_assoc($rs)) fputcsv($fh,$row); fclose($fh); ?> See the fine manual about fputcsv() Ken Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-538986 Share on other sites More sharing options...
Xurion Posted May 14, 2008 Share Posted May 14, 2008 My version gives you the csv to download. Your one stores it on the server. Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-540859 Share on other sites More sharing options...
abdfahim Posted May 22, 2008 Author Share Posted May 22, 2008 thanx man ... I normally used the Xurion's process .... but enjoy the function fputcsv()... greatz !!! by the way, for Xurion, you can change php.ini setting so that you can also download *.csv files in kenrbnsn's process. Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-547219 Share on other sites More sharing options...
mlin Posted May 22, 2008 Share Posted May 22, 2008 binary content transfer encoding? why is this? it's a text file. I would think you'd get the desired results omitting that header...am I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-547220 Share on other sites More sharing options...
abdfahim Posted May 29, 2008 Author Share Posted May 29, 2008 if you omit those header portion in normal php installation (without changing the php.ini file), it will open the text file in your browser rather than prompting for download/open. Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-552338 Share on other sites More sharing options...
nashsaint Posted June 15, 2008 Share Posted June 15, 2008 Hi, I used Xurion's method and it works great. I have one question though, I want the .csv file to be preformatted to specific cell width. how can I do that using php code? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-565890 Share on other sites More sharing options...
Xurion Posted June 18, 2008 Share Posted June 18, 2008 I assume you are referring to a spreadsheet software like Excel? The problem is that a .csv file contains data only, and it cannot store instructions for Excel (or which ever one you use) for cell widths. Only way I can see you doing this is to open the .csv file in your software, adjusting the widths, and saving the file in a different format. Quote Link to comment https://forums.phpfreaks.com/topic/105252-solved-writing-csv-file/#findComment-568053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.