mediabob Posted March 2, 2008 Share Posted March 2, 2008 Hi, I am trying to query a db table and save the results to a tab delimited text file, but I can't seem to figure out how to do it ??? can anyone help me with this it should be saved to a folder on the server and be formatted like head head head row row row sort of like a spreadsheet Thanks Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 2, 2008 Share Posted March 2, 2008 Give this a try mate see attached phpfreaks.txt being as it wont let me post the code i get a 403 permissions denied error on here :S that should do the trick, however this has not been tested. Regards Liam Quote Link to comment Share on other sites More sharing options...
mediabob Posted March 2, 2008 Author Share Posted March 2, 2008 OK I tried that and get error Parse error: syntax error, unexpected T_WHILE in ***/test.php on line 10 Here is my code I tried $result = mysql_query("SELECT * FROM mydb",$db); $csvcontent = 'field1'."\t".'field2'."\t".'field3'."\n\r". while ($row = mysql_fetch_array($result)) { $csvcontent .= $row['field1']."\t".$row['field2']."\t".$row['field3']."\n\r". } Quote Link to comment Share on other sites More sharing options...
mediabob Posted March 2, 2008 Author Share Posted March 2, 2008 forgot to add, I tried it with a single row and it worked, I just can't the loop to work to do all the records :-\ Quote Link to comment Share on other sites More sharing options...
Barand Posted March 2, 2008 Share Posted March 2, 2008 try <?php include 'db.php'; $sql = "SELECT * FROM tablename"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); $fp = fopen ('aaa.csv', 'w'); $row = mysql_fetch_assoc($res); $heads = array_keys($row); fputcsv($fp, $heads, ',', '"'); // write headings do { fputcsv($fp, $row, ',', '"'); // write data } while ($row = mysql_fetch_assoc($res)); fclose ($fp); ?> Quote Link to comment Share on other sites More sharing options...
mediabob Posted March 3, 2008 Author Share Posted March 3, 2008 I had to modify slightly but that worked, thanks Edit: What happed to the Solved button ??? Quote Link to comment 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.