开发者

Making a Message Appear if the Results of a Query Are Zero

I have a variable called $uid, and I would like to print a message if the variable $uid is not contained in the "loginid" field of a MySQL table "tweets" on any row that has another variable, $submissionid. I think my query $tweetquery is okay. Basically, I would like the message to echo if the loginid variables pulled from "tweets" never equal $uid.

How can I do this?

Thanks in advance,

John

$tweet开发者_Go百科query = "SELECT loginid
               FROM tweets 
              WHERE submissionid = '$submissionid'";


   $tweetresult = mysql_query($tweetquery);         

   if...

echo '<div>Message</div>';


Have a look at mysql_num_rows($result).

if ( mysql_num_rows($result) > 0 )
{
  // Do something
}
else
{
  echo 'No results';
}


Call me crazy, but using mysql_num_rows is not the way I'd do this. mysql_fetch_array does not do anything destructive if nothing is returned, so call that as normal.

$row = mysql_fetch_array( $resource );
if(!$row)
{
    //do what you would do if the query returned nothing
}
else
{
   do
   {
      // do what you would do with the row
   } while( $row = mysql_fetch_array( $resource ) );
}

The problem with mysql_num_rows is that it actually represents a call to the database asking for how many rows exist in the pointer. It is a, frankly, needless back-and-forth which can easily be avoided. So why not avoid it?


You can use mysql_num_rows:

if (mysql_num_rows($tweetresult) > 0)
     // results found


Use mysql_num_rows():

if (mysql_num_rows($tweetresult) == 0) {
    echo 'Message';
}


if(mysql_num_rows($tweetresult) == 0)
    echo "<div>Message</div>";
else {
    //Do something if $uid was found
}


mysql_num_rows() returns the numbers of rows fetched from the database. If rows fetched are zero echo a message.

$tweetquery = "SELECT loginid
           FROM tweets 
          WHERE submissionid = '$submissionid'";


$tweetresult = mysql_query($tweetquery);         

if(mysql_num_rows($tweetresult) > 0){
    // your results here
} else {

    echo '<div>Message</div>';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜