1

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

$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。

4

2 回答 2

3

你需要打电话$getEmail->store_result();

$mysqli = connectToDB();
$getEmail = $mysqli->prepare('SELECT * FROM users WHERE email=?');
$getEmail->bind_param('s', $email);
$getEmail->execute();
$getEmail->store_result();
$countRows = $getEmail->num_rows;
print $countRows;
于 2013-10-02T10:40:54.863 回答
2

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

$getEmail->execute();
$getEmail->store_result();
$countRows = $getEmail->num_rows;
于 2013-10-02T10:40:52.383 回答