hughesa1 Posted June 27, 2015 Share Posted June 27, 2015 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 2So 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? Link to comment https://forums.phpfreaks.com/topic/297065-select-query-is-only-selectingchecking-the-first-row-how-do-i-selectcheck-all/ Share on other sites More sharing options...
Ch0cu3r Posted June 27, 2015 Share Posted June 27, 2015 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 } Link to comment https://forums.phpfreaks.com/topic/297065-select-query-is-only-selectingchecking-the-first-row-how-do-i-selectcheck-all/#findComment-1515086 Share on other sites More sharing options...
fastsol Posted June 27, 2015 Share Posted June 27, 2015 If this is the actual code if(!empty($_SESSION[UserID]) and $a5['listingid'] == $_GET['id']) You can't use "and", it should be && Link to comment https://forums.phpfreaks.com/topic/297065-select-query-is-only-selectingchecking-the-first-row-how-do-i-selectcheck-all/#findComment-1515087 Share on other sites More sharing options...
jcbones Posted June 27, 2015 Share Posted June 27, 2015 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 Link to comment https://forums.phpfreaks.com/topic/297065-select-query-is-only-selectingchecking-the-first-row-how-do-i-selectcheck-all/#findComment-1515091 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.