mysqli_query()始终返回true [英] mysqli_query() always return true
本文介绍了mysqli_query()始终返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的表格:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="register_ajax.php" method="get">
<input type="text" name="email">
<input type="submit" value="test">
</form>
</body>
</html>
这是我的php代码:
<?php
$dbc = mysqli_connect("localhost","root","*******","continental_tourism") OR die(mysqli_connect_error());
$email = $_GET['email'];
$query = "SELECT email FROM customer_info WHERE email = '$email' ";
$r = mysqli_query($dbc, $query) OR die(mysqli_error($dbc));
if($r)
echo "Email address exists!";
else
echo "sss";
?>
如果我输入正确(db上的现有电子邮件),则$r
为true.但是,如果我输入不存在的电子邮件,那么$r
也是正确的.这是为什么?基本上我想检测空集.我该怎么办?
If I enter a correct(Existing email on db) $r
is true. But if I enter non existing email, then also $r
is true. Why is that? Basically I want to detect the empty set. How can I do it?
谢谢!
推荐答案
$r
仅在出现SQL错误时为false.否则,即使您的SELECT语句未返回任何行,它也将始终返回资源ID.
$r
will only be false if there was SQL error. Otherwise it will always return a resource ID even if no rows are returned by your SELECT statement.
使用 mysqli_num_rows()
计算返回的行数.零表示没有人正在使用该电子邮件地址.
Use mysqli_num_rows()
to count how many rows are returned. Zero means no one is using that email address.
if(mysqli_num_rows($r))
echo "Email address exists!";
else
echo "sss";
这篇关于mysqli_query()始终返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文