...

View Full Version : mysql_fetch_array fails bit does not send an error



yonni
07-20-2006, 07:16 PM
************************SOLVED*************************

Yeah... I have no idea how to approach this. I guess I'll show you the code first:


while($n<$rows) {
echo "<tr>";
$query="SELECT * FROM `survey` WHERE `id`=$id";
$data=mysql_query($query) or die ('<b>ERROR 5: unable to retrieve data from database: '.mysql_error().'. Please try again later.</b>');
$row=mysql_fetch_row($data) or die ('<b>ERROR 6: unable to fit data into an array: '.mysql_error().'. Please try again later.</b>');
//......
//code
//......
$n=$n+1;
}

I get "ERROR 6..." thrown at me, but there is no error message from mysql to display. If it helps, I'm using mysql v4 not v5. If you need more surrounding code, just ask, thanks.

Kid Charming
07-20-2006, 07:26 PM
mysql_error() won't work on fetch calls. m_e() is only to retreive an error message from the database when you use a function that directly communicates with that db (like mysql_connect(), mysql_query(), etc.). By the time you run a fetch function, you've already got a result set and are no longer working with the db itself.

You don't want to run a die() routine with a fetch function anyway, because a FALSE return value isn't necessarily an error. Fetch functions return FALSE when they don't receive a row from the result set (meaning you've iterated through all the rows, or no rows matched your query conditions).

To get to the bottom of your issue, I'd remove the fetch call's die() completely and run a mysql_num_rows() check on your result to set to see if you have any rows returned.

yonni
07-20-2006, 08:50 PM
Thanks for that, but I still don't think that the mysql_fetch_array is working, as i cant get anything from it. Basically im using (for example):

echo "<td>".$data[0];
and ive tried:

echo "<td>".$data['id'];
(where 'id' is the column header in the database), and neither are displaying anything.

Kid Charming
07-20-2006, 09:13 PM
Double-check you're getting results with mysql_num_rows().

You also need to refer to the array you're creating, not your result resource:
$row[0] instead of $data[0]. Also note that if you're using mysql_fetch_row(), you will only have numeric indices, so $row['id'] won't work. mysql_fetch_array() gives you numeric and string indices (and, to round it out, mysql_fetch_assoc() only gives string indices).

yonni
07-20-2006, 09:56 PM
I have already checked, and I am getting results (3 rows to be precise). And i did mean "$row[]" rather than "$data[]", that was just an accident in the transcription. Thankyou for clearing up of what each mysql_fetch_... means, I had read that somewhere before, but could not quite remember. However, although I am using mysql_fetch_row() and have tried using both numerical and string indices, I still get no data displayed. For reference, here is the whole php code:


<?php
$dbh=mysql_connect("***", "***", "***") or die ('<b>ERROR 1: Could not connect to the database: '.mysql_error().' Please try again later</b>');
mysql_select_db("johall0_1") or die ('<b>ERROR 2: unable to select the correct database: '.mysql_error().'. Please try again later.</b>');
$query="SELECT * FROM `survey`";
$fulldata=mysql_query($query) or die ('<b>ERROR 3: unable to retrieve data from database: '.mysql_error().'. Please try again later.</b>');
$rows=mysql_num_rows($fulldata);
$n;
$id=0;
while($n<$rows) {
echo "<tr>";
$query="SELECT * FROM `survey` WHERE `id`=$id";
$data=mysql_query($query) or die ('<b>ERROR 5: unable to retrieve data from database: '.mysql_error().'. Please try again later.</b>');
$row=mysql_fetch_row($data);
echo "<td>".$row[0];
echo "<td>".$row[1];
echo "<td>".$row[2];
echo "<td>".$row[3];
echo "<td>".$row[4];
echo "<td>".$row[5];
echo "<td>".$row[6];
echo "<td>".$row[7];
echo "<td>".$row[8];
echo "<td>".$row[9];
echo "<td>".$row[10];
echo "<td>".$row[11];
echo "<td>".$row[12];
echo "<td>".$row[13];
echo "<td>".$row[14];
echo "<td>".$row[15];
echo "<td>".$row[16];
echo "<td>".$row[17];
echo "<td>".$row[18];
echo "<td>".$row[19];
$n=$n+1;
}
?>

Kid Charming
07-20-2006, 10:10 PM
Each iteration, you're looking for a row with id 0 -- you have three rows with id 0?

yonni
07-21-2006, 07:31 AM
omg I cant believe i didnt see that. it's checking for something with id 0 each time, and i think mine start at id=8 or something (due to auto incriment). also, it's only looking 3 times (as there are only 3 rows). Ok, thanks for your help everyone, guess i just needed some sleep to work it out.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum