Page 1 of 1

Evidently a mysql_field_name Problem

Posted: Fri Jan 09, 2004 12:28 pm
by theoph
What is wrong with the following code that would give me this error in my downloaded file?

Warning: mysql_field_name(): Field 19 is invalid for MySQL result index 4 in /home/ekk-lexo/public_html/php/donations/download.php on line 18.

BTW - Field 19 is the last field in the search query and the above error will show up for every row in the found query. Data, however, comes through OK but I have all these errors listed as well.

Line 18 contains the following php code:

Code: Select all

$header .= mysql_field_name($rs_D, $i) . "\t";
The full script is below:

Code: Select all

<?php
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=donations.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
?>
<?php require_once('../../Connections/Ekklesia.php'); ?>
<?php
mysql_select_db($database_Ekklesia, $Ekklesia);
$query_rs_D = "SELECT donations.donor, donations.giftDate, donations.type, donations.checkNum, donations.genFund, donations.misFund, donations.benFund, donations.notes, oikos.id, oikos.company, oikos.salutation, oikos.family, oikos.greeting, oikos.famLabel, oikos.address, oikos.city, oikos.st, oikos.zip, oikos.household  FROM donations, oikos WHERE (donations.donor = oikos.id) AND (donations.giftDate > '2002-12-31') AND (donations.giftDate < '2004-01-01')";
$rs_D = mysql_query($query_rs_D, $Ekklesia) or die(mysql_error());
$row_rs_D = mysql_fetch_assoc($rs_D);
$totalRows_rs_D = mysql_num_rows($rs_D);
?>
<?php
	for ($i = 0; $i < $totalRows_rs_D; $i++)
	{
		$header .= mysql_field_name($rs_D, $i) . "\t";
	}
?>
<?php
	while ($row = mysql_fetch_row($rs_D))
	{
		$line = '';
		foreach($row as $value)
		{
			if ((!isset($value)) OR ($value == ""))
				{
					$value = "\t";
				}
			else
				{
					$value = str_replace('"', '""', $value);
					$value = '"' . $value . '"' . "\t";
				}
			$line .= $value;
		}
		$data .= trim($line)."\n";
	}
	$data = str_replace("\r","",$data);
?>
<?php if ($data == "") {$data = "\n(0) Records Found!\n";} ?>
<?php
	print "$header\n$data";
?>
<?php
mysql_free_result($rs_D);
?>

Posted: Sat Jan 10, 2004 6:52 am
by markl999
You're only selecting 18 'columns' but using the number of rows returned to loop over them.

Try using this instead :
$totalcols = count($rows_rs_D);
for ($i = 0; $i < $totalcols; $i++)

.. i used another var ($totalcols) so it doesn't have to evaluate the count on every interation.

Posted: Sat Jan 10, 2004 11:47 am
by theoph
Yep, that what I did. I now see the error of my ways. :wink: