Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql boolean error

Status
Not open for further replies.

yebaws

Technical User
Mar 22, 2007
42
GB
This is driving me nuts...

Ever since the php version on my server has been upgraded to ver5 I get error_log files appearing in all my sites that use databases with pages of entries like this:

PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/xxxx/public_html/xxxx.php on line 278

I never had these warnings before the php upgrade. And here's what's on line 35 of xxxx.php:

29>> $SQL = " SELECT * FROM coursedescriptions WHERE coursenumber = '$coursenumber'";
30>> $retid = mysql_query($SQL);
31>> if (!$retid) { echo( mysql_error()); }
32>>
33>>
34>>
35>> while ($row = mysql_fetch_array($retid)) {

The query is totally valid, (I can run it direct in phpmyadmin without an error) the script works fine and no error messages are displayed on the page. The only problem is that I get these error files appearing which get bigger and bigger and have to be deleted occasionally as they start to take up too much space...


Any help much appreciated....
 
Mysql_query returns a boolean value, that is true or false, in certain circumstances.

Since mysql_fetch_array expects an actual result set to work off of, passing it a true or false would generate that error.

The query is totally valid, (I can run it direct in phpmyadmin without an error) the script works fine and no error messages are displayed on the page
The script may be working, but the error tells me its not fine.
Your code does not stop execution of your script if an error shows up, so any error with the query may be getting obscured.
Try actually stopping the script when the query fails and you'll likely see an error.

Code:
30>> $retid = mysql_query($SQL);
31>> if (!$retid) { echo( mysql_error()); [red]die();[/red] }

I would also output the query being run at the time of the error so you know why its failing.
I suggest something like this, so you stop the query at the moment the error shows up:
Code:
$retid = mysql_query($SQL)[red] or die("Query Failed: " . mysql_error() . "<br>The query run was: " . $SQL)[/red];

The bottom line is at some point the query is failing or returning a value that mysql_fetch_array can't use.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
GOT IT! Thanks! The query, on occasion was coming up:

The query run was: SELECT full FROM coursepwds WHERE courseid = 2332013T.B.A.

Those .'s shouldn't be there...

For the future, is there any way that I can get PHP to write more error details to the log file to help diagnose problems without displaying errors on the page?

Thanks again...
 
You can use the trigger_error() function. Since your errors are getting logged, rather than displayed, this will log any error you want with as much information as you need.

For instance:

Code:
$retid = mysql_query($SQL);
 if (!$retid) {trigger_error('Query Failed: ' . mysql_error() . " the query being run was: " . $SQL) }

This will log all the information from the mysql error.
Of course this only works on non fatal errors. As those would stop execution wherever they happen.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top