mysql value not found
I have a mysql table and am using php. I have:
mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
Now this开发者_开发百科 returns an ID if email was found. However, I want to do something if it does not find $email in the email table column. How can I recognise when $email was not found and tell php?
mysql_query
returns a result. You can call mysql_num_rows
on that result to see the number of rows selected:
$result = mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
if (mysql_num_rows($result) > 0) {
// found a result
} else {
// no result found
}
I think that mysql_num_rows check should help.
If mysql_num_rows()
returns 0, no row was selected because no email matched. http://docs.php.net/mysql_num_rows
All that is required to check if a result was not found is an if statement used with the mysql_num_rows
function:
$check = mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
// No email found, so show an error to the user
echo "No email found!";
}
else
{
// An email address was found, so do something with it here
}
精彩评论