Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exporting Mysql table with php

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
0
0
BE
Hi,

I've seen a script that exports a Mysql Table into a CSV-file that you can open with Office Excel... But unfortunatly, I have no Idea how? Can Anyone be so kind as to tell me how it is done?

Thanks a million !!
math
 
if you want you own code

@$sub = fopen("./data/".$filename.".txt","w");
if (!$sub) echo "error creating ".$filename.".txt \n ";

@$result = mysql_query("select * from table", $db_connection);
$column_number = mysql_num_fields($result);
if (!$column_number) echo "error reading table header";

/*get column names*/
unset($header);
for($i=0;$i<$column_number;$i++) $header[$i] = mysql_field_name($result, $i);
fputs($sub,&quot;'&quot;.implode(&quot;','&quot;,$header).&quot;'\r\n&quot;);

/*get data*/
while($data = mysql_fetch_array($result))
{
unset($to_file);
for($i=0;$i<$column_number;$i++) $to_file[$i] = preg_replace(&quot;/\'/&quot;,&quot;''&quot;,stripslashes(trim($data[$header[$i]])));
fputs($sub,&quot;'&quot;.implode(&quot;','&quot;,$to_file).&quot;'\r\n&quot;);
}
fclose($sub);

hope this helps
 
An alternative would be to display your table in html, then you can use a 'web query' in Excel to link to it. (This has the benefit of being a dynamic link).

-Rob
 
Try
The freeware Windows program allows you (the site admin) to pop open your MySQL database and tweak it as if you were in Excel. You can import and export all the SQL and CSV files you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top