Jump to content

Archived

This topic is now archived and is closed to further replies.

punky79

mysql_num_fields() expects parameter 1 to be resource

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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;
		}


//}
//}
?>

Share this post


Link to post
Share on other sites

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;
		}


//}
//}
?>

Share this post


Link to post
Share on other sites

<?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;
		}


//}
//}
?>

 

Share this post


Link to post
Share on other sites

Yes that is perfect.  I was over complicating it.

 

Thank you for all your help :)

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.