Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正确识别数据库中是否已经存在电子邮件的问题-PHP_Php_Sql_Prepared Statement - Fatal编程技术网

正确识别数据库中是否已经存在电子邮件的问题-PHP

正确识别数据库中是否已经存在电子邮件的问题-PHP,php,sql,prepared-statement,Php,Sql,Prepared Statement,是否有人知道如何更正此代码,以便识别数据库中是否已存在电子邮件并显示错误。即使数据库中不存在电子邮件,它当前也会显示错误消息-因此表单不会被提交: $email = $_POST['email']; //prepare and set the query and then execute it $stmt = $conn2->prepare("SELECT COUNT(email) FROM users WHERE email = ?"); $stmt->bind_param('s'

是否有人知道如何更正此代码,以便识别数据库中是否已存在电子邮件并显示错误。即使数据库中不存在电子邮件,它当前也会显示错误消息-因此表单不会被提交:

$email = $_POST['email'];
//prepare and set the query and then execute it
$stmt = $conn2->prepare("SELECT COUNT(email) FROM users WHERE email = ?");
$stmt->bind_param('s', $email);
$stmt->execute();

// grab the result
$stmt->store_result();

// get the count
$numRows = $stmt->num_rows();

if( $numRows )
{
$errors = true;
 echo "<p class='red'>Email is already registered with us</p>";
}
else


//if we have no errors, do the SQL
$numRows=$stmt->num_行;将始终返回一行,因为即使COUNTemail的值为0,您也只选择了一行

$row = $stmt->fetch()
echo $row[0]; // result of COUNT(email)

选择计数*将始终返回1行

您需要使用SELECT*或调整代码以从SQL语句查询返回的结果,如下所示:

$stmt = $mysqli->prepare("SELECT COUNT(email) FROM users WHERE email = ?");
$stmt->bind_param('s',$email);
$stmt->execute();
$stmt->bind_result($count);
while($stmt->fetch()){}

if(!empty($count)){ echo "Already Registered"; }
使用:

// grab the result
$stmt->store_result();
// get the count
$numRows = $stmt->num_rows();
仅当您以前保持另一个连接打开时才使用

使用:

// grab the result
$stmt->store_result();
// get the count
$numRows = $stmt->num_rows();

不管怎样,当你只是计算id时,这是一种愚蠢的方法。

这段代码有什么问题?更有用的相关信息: