我有一个mysql表,我正在使用php。我有:
mysql_query("SELECT id FROM table WHERE email='$email' LIMIT 1");
现在,如果找到电子邮件,则返回ID。但是,如果在电子邮件表格列中找不到$ email,我想做点什么。如何识别找不到$ email的电子邮件并告诉php?
答案 0 :(得分:6)
mysql_query
返回结果。您可以在该结果上调用mysql_num_rows
以查看所选行数:
$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
}
答案 1 :(得分:1)
我认为mysql_num_rows检查应该有所帮助。
答案 2 :(得分:1)
如果mysql_num_rows()
返回0,则未选择任何行,因为没有匹配的电子邮件。 http://docs.php.net/mysql_num_rows
答案 3 :(得分:0)
检查是否找不到结果所需的全部内容是与mysql_num_rows
函数一起使用的if语句:
$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
}