Page 1 of 1

How to output an entire table from MYSQL?

Posted: Thu Jun 18, 2009 7:52 am
by becky-atlanta
I have a large table that I would like to display on a web page as a table or export it to a CSV format file. Does MYSQL have a function/command which allows me to output the entire table? I just try to see if I can get away from typing all 44 field names. Is it possible?

Re: How to output an entire table from MYSQL?

Posted: Thu Jun 18, 2009 8:11 am
by alexpaul_v
if you need the column ( field ) names of a table, then you can use this query: "SHOW COLUMNS FROM <table name>"

Re: How to output an entire table from MYSQL?

Posted: Thu Jun 18, 2009 8:14 am
by Mark Baker
You don't need to type the actual field names at all.

Code: Select all

 
$csvFile = fopen('myTable.csv','w');
$query = "select * from myTable";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
   $csvArray = array();
   foreach($row as $value) {
      $csvArray[] = $value;
   }
   fputcsv($csvFile,$csvArray);
}
fclose($csvFile);
 

Re: How to output an entire table from MYSQL?

Posted: Tue Jun 23, 2009 2:53 pm
by becky-atlanta
Thank you, alexpaul and Mark. You both gave excellent advice. I was able to do what I want in a much more efficient way. Thanks again!