Reputation: 137
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 ?
Upvotes: 0
Views: 102
Reputation: 1515
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.
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.
Upvotes: 1
Reputation: 2904
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
.
Upvotes: 1
Reputation: 6121
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;
}
}
Upvotes: 1