Jump to content

Recommended Posts

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

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

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

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

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.