Jump to content

SELECT query is only SELECTING/CHECKING the first row, how do I SELECT/CHECK all


hughesa1

Recommended Posts

I have this simple query:
 

 
$q5 = "select listingid FROM userlisting WHERE userid = '$_SESSION[UserID]'";
$r5 = mysql_query($q5) or die(mysql_error());
$a5 = mysql_fetch_array($r5);

The userlisting table is a many to many relationship. It is used as a lookup table. Each userid can be associated with multiple listingids and vice versa.

Also in my file (a html template file) I have this code
 

 
if(!empty($_SESSION[UserID]) and $a5['listingid'] == $_GET['id']) :

So I am wanting to check if the listingid column in userlisting table = the id of the page (well, it is a property website and it is the id of the property). As each user can be associated with many listingids I need to be able to check multiple rows. But for some reason it only checks the very first row.

In the userlisting table there is the following test entries: userid | listingid 1 1 1 2

So one user associated to two listingids. However, it is acting like this userid is only associated with listingid 1, the very first row.

 

Any solutions?

it is only checking the first row (returned by the query) because mysql_fetch_array() only returns 1 row a time, each time it is called the next row in the result is returned.

 

You need to perform your check within a while loop

while($a5 = mysql_fetch_array($r5))
{
    // perform check here
}

If this is the actual code

if(!empty($_SESSION[UserID]) and $a5['listingid'] == $_GET['id'])

You can't use "and", it should be &&

Yes you can use 'and', it is just a lower precedence than &&. http://http://php.net/manual/en/language.operators.logical.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.