PHP:使用mysqli查询检查行是否存在
我有两个函数用于创建用户。第一个函数用于插入/创建用户,另一个函数用于检查用户是否存在于PHP:使用mysqli查询检查行是否存在,php,mysqli,Php,Mysqli,我有两个函数用于创建用户。第一个函数用于插入/创建用户,另一个函数用于检查用户是否存在于MySql数据库中。但当我发送数据时,我会看到die()的空白页;(如果用户存在或用户不存在)如何修复此错误?以及如何在红色框中打印错误列表,即: - this user not activate - email is empty - .... 我的班级: public function createUser($email, $password, $salt, $verification) { i
MySql
数据库中。但当我发送数据时,我会看到die()的空白页;(如果用户存在或用户不存在)如何修复此错误?以及如何在红色框中打印错误列表,即:
- this user not activate
- email is empty
- ....
我的班级:
public function createUser($email, $password, $salt, $verification) {
if($this->checkUserFound($email) != true ){die();}
$query = "INSERT INTO tbUsers (email, password, user_salt, is_verified, is_active, is_admin, verification_code) "
. "VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $this->_db->prepare($query);
$ver = 0;
$act = 1;
$adm = 0;
$stmt->bind_param("sssiiis", $email, $password, $salt, $ver, $act, $adm, $verification);
if ($stmt->execute()) {
return true;
}
return false;
}
public function checkUserFound($email) {
$query = "SELECT email FROM tbUsers where email = ?";
$stmt = $this->_db->prepare($query);
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$stmt->bind_result($email);
//fetch first row of results
$stmt->fetch();
return false;
}
return true;
}
这将始终返回
FALSE
(假设查询成功执行):
您要将其更改为:
if ($stmt->execute()) {
$stmt->store_result();
return 0 == $stmt->num_rows ? FALSE : TRUE;
}
然后,如果找到用户,它将返回TRUE
;如果不是,则返回FALSE
此外,我认为(从语义上)这样做更有意义:
if($this->checkUserFound($email)){die();}
。。。因为您希望它在找到用户时返回
TRUE
(并结束脚本,因为您不想插入重复的内容)。检查mysqli\u num\u行
:0如果不存在,1如果存在。@enapupe:在checkUserFound函数中?或者在if($this->checkUserFound($email)!=true){die();}
?!在checkUserFound中,只有当SQL有一个结果行时,它才应该返回true(或用户ID)。。我不知道您正在使用的类,但您必须具有一些函数,如$stmt->num_rows()。。不管怎样,为什么函数总是返回false??
if ($stmt->execute()) {
$stmt->store_result();
return 0 == $stmt->num_rows ? FALSE : TRUE;
}
if($this->checkUserFound($email)){die();}