$result = mysql_query("..."); if($row = mysql_fetch_array($result)) { do { //loop through results }while($row = mysql_fetch_array($result)); } else { echo "No rows returned"; }
You could also just check mysql_num_rows() to see if any were returned and base your if off of that. ---John Holmes... ----- Original Message ----- From: "Chris Knipe" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, May 13, 2002 10:46 AM Subject: [PHP] How *not* to assume MySQL will return data? > Hiya all, > > Stock standard, and from most of the examples I've seen, standard MySQL > lookups via PHP utlises a while look to retrieve the data. All very nice > and well yes... But what if there is no data? > > Here's a little example of what I want to do... > > // Check for inportaint notices for the contact. > echo "<table width=\"100%\" class=\"td\">\n"; > echo " <tr>\n"; > echo " <td colspan=\"2\" valign=\"top\" class=\"subhead\">\n"; > echo " <b><u>Important notes:</u></b><br>\n"; > echo " </td>\n"; > echo " </tr>\n"; > $SQL = mysql_query("SELECT MessageID, MessageTitle FROM contactmessages > WHERE ContactID='" . $_SESSION['ContactID'] . "' AND MessageRead='n';"); > while ($ContactMessageList = mysql_fetch_array($SQL)) { > echo " <tr>\n"; > echo " <td align=\"left\" valign=\"top\"><a > href=\"/profile/readmsg.php?MessageID=" . $ContactMessageList['MessageID'] . > "\" alt=\"Read Message\" border=\"0\"><img src=\"/lib/images/arrow.gif\" > border=\"0\"></td>\n"; > echo " <td align=\"left\" valign=\"top\">" . > $ContactMessageList['MessageTitle'] . "</td>\n"; > echo " </tr>\n"; > } > echo "</table>\n"; > echo "<br>\n"; > > >From this example, this "block" would be printed evertime. Whether the > contact has actual messages or not. How would I go about in a instance like > this, to use a MySQL check and only print the block if $SQL will actually > return data ? > > In this case, can I do something like... > > if ($SQL = mysql_query("something") { > print the block > while (do the actualy mysql lookup) { > print data > } > } > > Spanks everyone :) > > -- > me > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php