0

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /rideanddrive/_classes/__base.php on line 45

My code :

public static function SelectSpecific($obj, $data_base, $cond)
{
    self::$query = "select $obj from " . self::$baseprefix . "$data_base where $cond";
    $out = self::Execute();
    $array = mysql_fetch_array($out); // THIS LINE IS WARNING LINE
    return $array[$obj];
}

Execute function, if needed :

private static function Execute() 
{
    $out = mysql_query(self::$query);
    return $out;
}

Code's works fine. I don't get a warning on localhost (running WAMP, might have turned them off ?), it also works on live server, however I'm getting the above warning, how do I correct it so the warning is gone ?

3 Answers 3

1

On Your SelectSpecific function embed a if check to see your execute function returning proper resource for result .. if not then echo your sql then run it on server separately so that if you can find a error if there is any error with sql ,here is the updated function with if embedded

public static function SelectSpecific($obj, $data_base, $cond)
    {
        self::$query = "select $obj from " . self::$baseprefix . "$data_base where $cond";
        $out = self::Execute();
        if($out){
          $array = mysql_fetch_array($out); // THIS LINE IS WARNING LINE
          return $array[$obj];
       }else {
         return false;
       }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

@Abbys: Note, that this makes the error message not appear any more, but - of course - it does not fix the error in your SQL query.
@Callidior - thanks for the comment. However, this is an warning, not an error. The query itself works fine, it collects the data I wanted and returns it just fine. Or am I just missing something here ?
@Abbys: If you implemented it as in the answer above, no data is returned, but false in this case. So, there is at least one erroneous query. You have just added an explicit check, whether it was successful or not. If not, you don't even call mysql_fetch_array, which produced the warning; but you won't collect any data this way too.
1

This warning indicates, that there is - for some reason - a (syntactic or semantic) problem with your SQL query, so that mysql_query returns FALSE instead of a MySQL result resource.

You can use mysql_error() to retrieve the error message from MySQL as string for debugging.


Please note, that the mysql extension you're using is deprecated as of PHP 5.5 and will be removed in future releases of PHP. Consider switching to mysqli.

Comments

1

Your query probably returns FALSE because of an error.

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

Source

var_dump() your $out to see if it's FALSE.

And even better, just display your error to debug:

if (!$out) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

BTW, you shouldn't use mysql_query() and all other mysql_* functions anymore as they are deprecated. Use mysqli or PDO instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.