Double query and fetch_array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zavin
Forum Newbie
Posts: 11
Joined: Fri Feb 29, 2008 6:28 pm

Double query and fetch_array

Post by Zavin »

I need a little help with something I am doing wrong. In this code the first 2 lines selects the table called bans and returns ID and REASON. The last 2 lines should look at the table users, find the ID that matches the bans ID and return the users name. The error I am getting is Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in the 4th line. Any help would be appreciated.

Code: Select all

$result = mysql_query("SELECT * FROM `bans`");
while($line = mysql_fetch_array($result, MYSQL_ASSOC))
    $result2 = mysql_query("SELECT * FROM 'users' WHERE 'id' ='".$line['id']."'");
    $line2 = mysql_fetch_array($result2);
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Double query and fetch_array

Post by flying_circus »

Zavin wrote:I need a little help with something I am doing wrong. In this code the first 2 lines selects the table called bans and returns ID and REASON. The last 2 lines should look at the table users, find the ID that matches the bans ID and return the users name. The error I am getting is Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in the 4th line. Any help would be appreciated.

Code: Select all

$result = mysql_query("SELECT * FROM `bans`");
while($line = mysql_fetch_array($result, MYSQL_ASSOC))
    $result2 = mysql_query("SELECT * FROM 'users' WHERE 'id' ='".$line['id']."'");
    $line2 = mysql_fetch_array($result2);

Zavin, did you get this figured out?

It seems like you've got something funky going on here. Also, I've had MySQL complain about using single-quotes around db table and field names. Generally, in an instance like this, I would echo out the actual MySQL query and then try to run it manually on the SQL server. This helps to determine if the problem is PHP or SQL related. Generally, it's best to test the query for a result as well.

Try this:

Code: Select all

 
$result = mysql_query("SELECT * FROM bans");
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $result2 = mysql_query("SELECT * FROM users WHERE id ='" . $line['id'] . "'");
  if(mysql_num_rows($result2)) {
    $line2 = mysql_fetch_array($result2, MYSQL_ASSOC);
  }
}
 
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Re: Double query and fetch_array

Post by bdlang »

Code: Select all

 
SELECT * FROM 'users' WHERE 'id'
 
Your query is invalid; if you had run mysql_error() it would have returned an error message regarding that statement. In a nutshell, you've wrapped the table / column names in quotes and are attempting to use strings in the query. Remove the quotes, and if you must, replace them with `backticks`.

The real underlying issue here is that you're running multiple queries in loops rather than performing a proper JOIN on the two tables.

MySQL Manual : JOIN syntax
Post Reply