为什么mysqli_num_rows
函数总是在下面的代码中返回false?
<?php
if($_POST['submit']){
//to validate the email address
if(!$_POST['email']){
$error.="<br/>Please enter your email";
}else if(!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
$error.="<br/>Please enter a valid email address";
}
// To validate password
if(!$_POST['password']){
$error.="<br/>Please enter a password";
}else{
if(strlen($_POST['password'])<8){
$error.="<br/>Please enter a password with at least 8 characters";
}
if(!preg_match('`[A-Z]`',$_POST['password'])){
$error.="<br/>Please include at least one capital letter in your password";
}
}
if($error){
echo "There were error(s) in your sign up details".$error;
}else{
$link=mysqli_connect("localhost","diary","");
if(mysqli_connect_error()){
die("there is an arror");
}
//$query="insert into `users` (`email`,`number`) values ('asd@asad.com',123)";
$email1=$_POST['email'];
// search for already registered emails
$query="select * from users where email='".mysqli_real_escape_string($link,$email1)."'";
$result = mysqli_query($link, $query);
if (!$result){
die(mysqli_error($link));
}
// And here where errors comes.
$results = mysqli_num_rows($result);
if(!$results){
echo "That email address is already registered";
}else{
echo "dont know";
}
}
}
?>
<form method=post>
<input type="email" name="email" id="email"/>
<input type="password" name="password" />
<input type="submit" name="submit" value="Sign Up">
</form>
答案 0 :(得分:0)
如果这不是你的整个脚本,你需要在将内容连接到它之前将错误初始化为某个值。
$error = "";
其次,在将查询结果提供给mysqli_num_rows之前,您不必费心检查查询结果。在尝试对其执行任何其他操作之前,您应始终检查查询的值。
$query="select * from users where email='".mysqli_real_escape_string($link,$email1)."'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
if (!$result) {
// you should write this to an error log. don't show it to a user
$error = mysqli_error($link);
die("The query failed!");
}
// And here where errors comes.
$results = mysqli_num_rows($result);