punky79 Posted June 30, 2010 Share Posted June 30, 2010 Hi, I am trying to download a mysql query to a csv file and have written code to perform this. I can get the database information out to the file but the field names (headers) will not come out. I am using "mysql_num_fields" to get the number of field names but I get the error "mysql_num_fields() expects parameter 1 to be resource". I have checked the PHP manual and it seems that the mysqli query I am running returns an object and "mysql_num_fields()" needs a resource? I am not sure how to get around this? Here is my code...any help would be appreciated. Thanks require_once ('includes/database_connect.php'); $query = "SELECT * FROM cip ORDER BY cip_ID"; $result = mysqli_query ($dbc,$query); $num_fields = mysql_num_fields($result); $headers = array(); for ($i = 0; $i < $num_fields; $i++) { $headers[] = mysql_field_name($result , $i); } $fp = fopen('php://output', 'w'); if ($fp && $result) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="All CIPs report.csv"'); header('Pragma: no-cache'); header('Expires: 0'); fputcsv($fp, $headers); while ($row = $result->fetch_array(MYSQLI_NUM)) { fputcsv($fp, array_values($row)); } die; } Quote Link to comment Share on other sites More sharing options...
Mchl Posted June 30, 2010 Share Posted June 30, 2010 Your query probably failed. Use mysqli_error to find out why When using mysqli_query, you need to use mysqli_num_fields() not mysql_num_fields Quote Link to comment Share on other sites More sharing options...
punky79 Posted June 30, 2010 Author Share Posted June 30, 2010 Thank you for the very fast reply. That has worked for me but now an error on "mysql_field_name()" error "mysql_field_name() expects parameter 1 to be resource" I have tried mysqli_field_name() but it does not work. Should I be coming at it a different way to print out each header? Quote Link to comment Share on other sites More sharing options...
Mchl Posted June 30, 2010 Share Posted June 30, 2010 Actually, your best choice would be to use mysqli_fetch_fields() Quote Link to comment Share on other sites More sharing options...
punky79 Posted June 30, 2010 Author Share Posted June 30, 2010 Thanks for the reply. I am not sure how to use the "mysqli_fetch_fields" function. I have posted code below....but I think I have gone at it in the wrong way all together.... error "Object of class stdClass could not be converted to string" <?php //if(isset($_POST['submitted'])) //{ //echo "submitted"; //if(isset($_POST['csvexport'])){ require_once ('includes/database_connect.php'); $query = "SELECT * FROM cip ORDER BY cip_ID"; $result = mysqli_query ($dbc,$query); $num_fields = mysqli_num_fields($result); $headers = null; ///for ($i = 0; $i < $num_fields; $i++) { //$headers[] = mysql_field_name($result , $i); //} $fields = mysqli_fetch_fields($result); for ($i = 0; $i < $num_fields; $i++) { foreach ($fields as $val) { $headers .= $val; } } $fp = fopen('php://output', 'w'); if ($fp && $result) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="All CIPs report.csv"'); header('Pragma: no-cache'); header('Expires: 0'); fputcsv($fp, $headers); while ($row = $result->fetch_array(MYSQLI_NUM)) { fputcsv($fp, array_values($row)); } die; } //} //} ?> Quote Link to comment Share on other sites More sharing options...
punky79 Posted June 30, 2010 Author Share Posted June 30, 2010 Sorry I realised that I needed to just pass the name of the field to the array...not the whole object. I have modified the code. Now I am stuck in the foreach loop........ <?php //if(isset($_POST['submitted'])) //{ //echo "submitted"; //if(isset($_POST['csvexport'])){ require_once ('includes/database_connect.php'); $query = "SELECT * FROM cip ORDER BY cip_ID"; $result = mysqli_query ($dbc,$query); $num_fields = mysqli_num_fields($result); $headers = array(); ///for ($i = 0; $i < $num_fields; $i++) { //$headers[] = mysql_field_name($result , $i); //} $fields = mysqli_fetch_fields($result); for ($i = 0; $i < $num_fields; $i++) { foreach ($fields as $val) { $headers[$i]= $val->name; } } $fp = fopen('php://output', 'w'); if ($fp && $result) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="All CIPs report.csv"'); header('Pragma: no-cache'); header('Expires: 0'); fputcsv($fp, $headers); while ($row = $result->fetch_array(MYSQLI_NUM)) { fputcsv($fp, array_values($row)); } die; } //} //} ?> Quote Link to comment Share on other sites More sharing options...
Mchl Posted June 30, 2010 Share Posted June 30, 2010 <?php //if(isset($_POST['submitted'])) //{ //echo "submitted"; //if(isset($_POST['csvexport'])){ require_once ('includes/database_connect.php'); $query = "SELECT * FROM cip ORDER BY cip_ID"; $result = mysqli_query ($dbc,$query); $fields = mysqli_fetch_fields($result); $headers = array(); foreach ($fields as $field) { $headers[] = $field->name; } $fp = fopen('php://output', 'w'); if ($fp && $result) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="All CIPs report.csv"'); header('Pragma: no-cache'); header('Expires: 0'); fputcsv($fp, $headers); while ($row = $result->fetch_array(MYSQLI_NUM)) { fputcsv($fp, array_values($row)); } die; } //} //} ?> Quote Link to comment Share on other sites More sharing options...
punky79 Posted June 30, 2010 Author Share Posted June 30, 2010 Yes that is perfect. I was over complicating it. Thank you for all your help 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.