PHP准备好的语句-检查值是否已经存在 [英] PHP prepared statements - check if a value already exists

查看:58
本文介绍了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;

这篇关于PHP准备好的语句-检查值是否已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆