Jump to content

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


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

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.