Jump to content

mysql_num_fields() expects parameter 1 to be resource


punky79

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

Link to comment
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?

Link to comment
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;
		}


//}
//}
?>

Link to comment
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;
		}


//}
//}
?>

Link to comment
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;
		}


//}
//}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.