-1

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

I'm having a problem with this mysql code. I presume 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

Community
  • 1
  • 1
Niall Paterson
  • 3,580
  • 3
  • 29
  • 37
  • `$results=mysql_query($sqls) or die('Error: '. mysql_error());`. Also, use PDO instead of mysql_* functions, they're obsolete now. – N.B. Aug 18 '11 at 08:44
  • @N.B., although I agree that PDO would be a better option for new projects, I disagree that `mysql_*()` are obsolete. Officially they are **not obsolete**, it is just a subjective opinion. – binaryLV Aug 18 '11 at 08:49
  • @binaryLV - that's just a matter of semantics, they are obsolete. I assume you've used PDO and mysqli so I won't get into the huge discussion saying things that you already know. People should start avoiding mysql_* functions, not only due to security reasons but for the sake of learning how to code in a way that's efficient and easier to read when other people get involved in their code. – N.B. Aug 18 '11 at 08:54
  • I think it would be better for him to focus on writing better looking and more readable code overall than switching to PDO. – Kokos Aug 18 '11 at 08:59

3 Answers3

3

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
}
RiaD
  • 46,822
  • 11
  • 79
  • 123
3
$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 ;)

Kokos
  • 9,051
  • 5
  • 27
  • 44
  • 2
    implode(',',array_map(function($x){return "'".$x."'"}, $row)); is more readable:) – RiaD Aug 18 '11 at 08:47
0

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

J0HN
  • 26,063
  • 5
  • 54
  • 85