PHP准备好的语句-检查值是否已经存在

PHP准备好的语句-检查值是否已经存在

问题描述:

我试图检查一个值(电子邮件),以确定它是否已存在于数据库中.这应该使用准备好的语句来完成.这样做的最佳方法是什么?我的解决方法是这样(这是错误的):

I am trying to do a check on a value (email), to determine if it already exist in the db. This should be done using prepared statements. What is the best way for doing this? My solution is this (which is wrong):

$mysqli = connectToDB();
$getEmail = $mysqli->prepare('SELECT * FROM users WHERE email=?') or die('Couldn\'t check the email');
$getEmail->bind_param('s', $email);
$getEmail->execute();
$countRows = $getEmail->num_rows;
print $countRows;

即使数据库中存在电子邮件区域,它始终会打印出0.

It always prints out 0 even though the email aready exists in the db.

似乎您对无缓冲结果有疑问.您应该使用store_result:

It seems that you have an issue regarding unbuffered result. You should use store_result:

$getEmail->execute();
$getEmail->store_result();
$countRows = $getEmail->num_rows;