Bruce Gust
asked on
Export Mysql Table to CSV Using PHP - What's Wrong?
I liked this example because it was intuitive and matched my needs: http://www.a2zwebhelp.com/export-data-to-csv
Problem is, it's using mysql as opposed to mysqli so here's what I've got:
There's more than one problem here, but the first thing I want to tackle is my column names.
The example I liked is using a deprecated function, but I can't figure out how to get mysqli_fetch_fields working. I know it's supposed to be an array, but every time I try to write it as $heading=mysqli_fetch_fiel ds($michel le_query, $i), I get "Warning: mysqli_fetch_fields() expects exactly 1 parameter, 2 given in C:\wamp\www\json\csv_test. php on line 19"
Where am I blowing it?
Problem is, it's using mysql as opposed to mysqli so here's what I've got:
set_time_limit(600);
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
$output="";
$michelle="select * from verizon_test order by id";
$michelle_query=mysqli_query($cxn, $michelle)
or die("Michelle didn't work");
$michelle_columns=mysqli_field_count($cxn);
//gets the field names from your table and sets them up as the first row in your csv file
for($i=0; $i<$michelle_columns; $i++)
{
$heading=mysqli_fetch_fields($michelle_query);
$output .='"'.$heading.'",';
}
$output.="\n";
/*
while($michelle_row=mysqli_fetch_array($michelle_query))
{
for($i=0; $i<$michelle_columns; $i++)
{
$output .='"'.$michelle_row["$i"].'",';
}
$output.="\n";
}
$filename="json.csv";
header('Content-type:appliation/csv');
header('Content-Dispoition: attachment; filename='.$filename);
echo $output;
exit;*/
There's more than one problem here, but the first thing I want to tackle is my column names.
The example I liked is using a deprecated function, but I can't figure out how to get mysqli_fetch_fields working. I know it's supposed to be an array, but every time I try to write it as $heading=mysqli_fetch_fiel
Where am I blowing it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Don't know - I can't test the code I posted. Suggest you use var_dump() to print out the variables that are used on line 20.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm getting an error at line 20: "fputcsv() expects parameter 1 to be resource, string given in C:\wamp\www\json\csv_test.
What am I missing?