Web Hosting Talk







View Full Version : PHP & mysql_fetch_array();


robk
08-08-2003, 10:23 AM
Hi,

Can anyone see a bug here?


$db_conn = mysql_connect("localhost","sfsdfsdfds","dsfdsfdsf") or die(mysql_error());
mysql_select_db("dsfsdfsdsdf", $db_conn) or die(mysql_error());
$result = mysql_query("SELECT * FROM urls") or die(mysql_error());
$num_results = mysql_num_rows($result) or die(mysql_error());
for ($i=0; $i <$num_results; $i++)
{
$qry = mysql_fetch_array($result) or die(mysql_error());
print $qry[test];
}


It dies on this line:

$qry = mysql_fetch_array($result) or die(mysql_error());


The error message is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/path/script.php on line ###


Thanks,

Rob

i am a
08-08-2003, 10:37 AM
move the mysql_fetch_array out of the loop, a "better" setup may be:


while ($qry = mysql_fetch_array($result) ) {
print $qry['test'];
}

Burhan
08-08-2003, 10:46 AM
few things :

- add error_reporting(E_ALL);

the error you are getting is returned when the query did not return a valid result.

Try

if (!($foo = mysql_query($result)))
{
die(mysql_errno()." : ".mysql_error());
}

psychotesis
03-13-2006, 10:56 PM
I believe the problem is with the SQL statement. I've tried and its ok.

But when I forced an incorrect statement, $result becomes invalid and hence mysql_fetch_array($result) wouldn't work.

zeeshannaqvi
03-15-2006, 02:41 PM
Please print the number of rows. It seems like the sql is returning no records. and please replace the for loop with while as suggested by i am a

SiliconWolf
03-16-2006, 02:39 AM
You might need to add a second argument to your mysql_query call.

$result = mysql_query("SELECT * FROM urls",$db_conn)

It's not technically required, but there can sometimes be problems if you don't use it.