fife Posted November 18, 2010 Share Posted November 18, 2010 Hi I was wondering if someone could help. There is some code I would like but I have no idea what to search the net for to learn it. Basically I have a query which finds a record in my database. Now, I want another query which says that if there are more than two or more records in the database returned display table 1, if less than 2 display table 2. Can someone please point me in the right direction? Link to comment https://forums.phpfreaks.com/topic/219076-what-to-search-for/ Share on other sites More sharing options...
Pikachu2000 Posted November 18, 2010 Share Posted November 18, 2010 That should be simple enough to code yourself with a conditional, and counting the rows returned by the query. if( mysql_num_rows($result) > 1 ) { // display table 1 } else { // display table 2 } Link to comment https://forums.phpfreaks.com/topic/219076-what-to-search-for/#findComment-1136078 Share on other sites More sharing options...
fife Posted November 18, 2010 Author Share Posted November 18, 2010 Hi right here is the code I have. For some reason it is not working but not erroring either. Im basically trying to show a table but only if there are 2 or more records in the database. <?php $qGetUsers = "SELECT * FROM business WHERE cat1='".$BusinessInfo['cat1']."' AND businessid!='".$BusinessInfo['businessid']."' ORDER BY rand() LIMIT 4"; $rGetUsers = mysql_query($qGetUsers) or die(mysql_error()); while($allUsers = mysql_fetch_array($rGetUsers)) if( mysql_num_rows($rGetUsers) => 2 ) { ?> <p>Display table</p> <?php } else { ?> <p>No more available</p> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/219076-what-to-search-for/#findComment-1136097 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 1. have you connected to the db? 2. where are your getting the values for $BusinessInfo['cat1'] and $BusinessInfo['businessid']? 3. your coding needs a little work. (bleary eyed and untested) <?PHP /* make db connection */ /* set variable values here */ $bus_cat = some value; $bus_id = some value; /* create query here */ $query = "SELECT * FROM business WHERE cat1= '$bus_cat' AND businessid='$bus_id' ORDER BY rand() LIMIT 4"; /* execute the query here */ $result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows=> 2) { // do this }else{ // do that } ?> Link to comment https://forums.phpfreaks.com/topic/219076-what-to-search-for/#findComment-1136114 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.