Viewed   100 times

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I'm very confused with this error, it shows when I try to return a result from the DB that doesn't exist ... I tried mysql_num_rows() but it returns the same error but instead of mysql_fetch_assoc expects ... it says mysql_num_rows() expects ...

I set error_reporting(0) to avoid showing this error, but I'm not satisfied with this solution ...



Here's the proper way to do things:

$sql = 'some query...';
$result = mysql_query($q);

if (! $result){
   throw new My_Db_Exception('Database error: ' . mysql_error());

while($row = mysql_fetch_assoc($result)){
  //handle rows.

Note the check on (! $result) -- if your $result is a boolean, it's certainly false, and it means there was a database error, meaning your query was probably bad.

Monday, November 28, 2022

This means the MySQL query did not execute correctly. You can use this to figure out what went wrong:

$rs = mysql_query( "select * from users where fb_userid='$fb_user'" ) 
or die(mysql_error());

although in this case it seems the problem is that you have not enclosed table and column names within the required ` marks, so try

$rs = mysql_query( "select * from `users` where `fb_userid`='$fb_user'" ) 
or die(mysql_error());
Monday, December 5, 2022

Probably mysql_query() failed. In this case it returns false. Use mysql_error() to find out, what happens.

Tuesday, August 30, 2022

For warnings to be "flagged" to PHP natively would require changes to the mysql/mysqli driver, which is obviously beyond the scope of this question. Instead you're going to have to basically check every query you make on the database for warnings:

$warningCountResult = mysql_query("SELECT @@warning_count");
if ($warningCountResult) {
    $warningCount = mysql_fetch_row($warningCountResult );
    if ($warningCount[0] > 0) {
        //Have warnings
        $warningDetailResult = mysql_query("SHOW WARNINGS");
        if ($warningDetailResult ) {
            while ($warning = mysql_fetch_assoc($warningDetailResult) {
                //Process it
    }//Else no warnings

Obviously this is going to be hideously expensive to apply en-mass, so you might need to carefully think about when and how warnings may arise (which may lead you to refactor to eliminate them).

For reference, MySQL SHOW WARNINGS

Of course, you could dispense with the initial query for the SELECT @@warning_count, which would save you a query per execution, but I included it for pedantic completeness.

Tuesday, October 11, 2022

In some modes, MySQL raises warnings instead of the errors that are required by the SQL Standard.

You certainly want to raise an error when significant data is removed, but you probably don't want an error raised when a COUNT(colname) expression finds some rows with nulls in the column and ignores them for counting purposes (a Standard warning).

As MySQL becomes more conformant to the Standard, warnings that do not have data integrity implications may be added.

If using MySQL in a mode that more closely follows the SQL Standard is not enough, you could catch the unacceptable warnings in your application code and add appropriate compensating actions.

Friday, September 9, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :