Page 1 of 1

help with code please

Posted: Fri Feb 06, 2004 7:00 am
by andrew99
I have two syntax problems with php.

First problem :

Code: Select all

<?php

$db = mysql_connect(...);
mysql_select_db("...",$db);
$myquery= "SELECT * FROM ..... ";
$result = mysql_query($myquery);
$num_results=mysql_num_rows($result);

?>
And i get the following error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in line $num_results=...
I have used the above code many times in the same site, and although it works fine in the other pages, in this one i get the error.
Any suggestions or hints???

Second problem

Code: Select all

<?php

$myquery2 = "SELECT * FROM .......... ";
$sql = mysql_query($myquery2);
while($row = mysql_fetch_array($sql))
{ 
// format results here
}

?>
Same problem as above with error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Also this code works in other pages, not on this one.

Any suggestions or tips??

Thanks

Posted: Fri Feb 06, 2004 7:03 am
by twigletmac
You need to add error handling to your database calls, e.g.:

Code: Select all

$result = mysql_query($myquery) or die(mysql_error().'<p>'.$myquery.'</p>');
That way you should get a better indication of what the problem is.

Mac

Posted: Fri Feb 06, 2004 7:22 am
by andrew99
thanks it worked

stupid error actually, i was requesting data from a column that didn't exist.

By the way, can i put this error check in all commands in php? or vars?
where can i obtain more info on error checks?