开发者

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

I'm having a problem with this mysql code. I presum开发者_Python百科e its a basic error in the $sqlx... line but I'm slightly lost.

The code basically prints messages from a db Here is the code:

$sqls="SELECT username FROM social WHERE `adder`='$username'";
$results=mysql_query($sqls);
$resulti= mysql_num_rows($results);
if ($resulti==0) {
echo "You haven't added anyone yet. Find some <a   href=\"/social/suggestions\">suggestions</a>";
}
$row=mysql_fetch_array($results);

$sqlx="SELECT * FROM messages WHERE `sender` IN ($row)";
$resultx= mysql_query($sqlx); 
$resultz= mysql_num_rows($resultx);
if ($resultz==0){
echo "No messages at all!!";
}
else {
$finished="false";
$r=0;
While(($rowx=mysql_fetch_assoc($resultx))&&($finished=="false")) {
//echo off messages

$username is got further up the file.

Here is the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/user/public_html/social/iframe/index.php on line 34

Line 34 is $resultz= mysql_num_rows($resultx); But like i said the error is probably the line two up from that.

One interesting happens. "No messages at all!!" is echoed out which means the result of the mysql_query is 0. This is why I am convinced it is the line 32, ($sqlx)

Any idea??

Have I done the mysql_fetch_array wrong when getting $row?? thanks


$row=mysql_fetch_array($results);

$sqlx="SELECT * FROM messages WHERE `sender` IN ($row)";

This will create the following query:

SELECT * FROM messages WHERE `sender` IN (Array)

This is obviously not a valid MySQL query. You have to process the array.

$sqlx = "SELECT * FROM `messages` WHERE `sender` IN ("; // start of query

foreach($row as $r)
    $sqlx .= "'".$r['username']."',"; // insert all returned usernames

$sqlx = substr($sqlx,0,-1).')'; // substract the last comma and close the query

Or, as RiaD pointed out in the comments:

$sqlx = "SELECT * FROM `messages` WHERE `sender` IN (".
        implode(',',array_map(function($x){return "'".$x['username']."'"; }, $row)).
        ")";

PS: Riad, it should be $x['username'] instead of $x and you forgot the semicolon ;)


mysql_query($sqlx) return false instead of result. It means any error occured. Try to check is $sqlx correct query and check mysql_error() to get what error is occured. To check was here any error or not you can use

if(!$resultx){
     print 'error:'.mysql_error();
}
else{
    //use result
}


If your query fails mysql_query($sqlx) returns false rather than resource. So, you need to check, that this function returned true (e.g. if (!results) {}) nad print use mysql_error() to see what error was.

if(!$resultx){
   print 'error:'.mysql_error();
}

Also, you are embedding $row variable into the query string. But this var is an Array, so you end up with a query like this:

SELECT * FROM messages WHERE `sender` IN (Array)

See mysql_fetch_array manual for details

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜