我正在尝试使用一个简单的准备语句,其目的是在提供电子邮件ID时从表“用户”中查找ID,检查表用户中是否不存在该电子邮件ID。下面
$email='shyam@gmail.com';
$stmt = $conn->prepare("SELECT id FROM users WHERE email= ?");
$stmt->bind_param("s",$email);
$stmt->execute();
$stmt->store_result();
$result = $stmt->num_rows;
if($result >0){
echo "True";
}else {
echo "False";
}
此代码在一个简单的test.php上运行良好,因为我的电子邮件具有上述值,所以当我没有其他电子邮件时,它返回true,反之亦然。
public function createNewUser($email, $password){
if(! $this->isEmailExist($email)){
$stmt = $this->conn->prepare("INSERT INTO users (email, password) VALUES (?,?)");
$stmt->bind_param("ss",$email,$password);
if($stmt->execute()){
return USER_CREATED;
}else {
return USER_FAILURE;
}
}
return USER_EXISTS;
}
//Check whether the email already exists
private function isEmailExist($email){
$stmt = $this->conn->prepare("SELECT id FROM users WHERE email= ?");
$stmt->bind_param("s",$email);
$email = $this->email;
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
因为我使用的是苗条框架...。我未使用Illuminate数据库..我正在尝试从Android设备以及使用POSTMAN发布这些数据...我遇到了以下错误:-
Type: Error
Message: Call to a member function prepare() on null
File: C:\wamp64\www\myapi\includes\DbOperations.php
Line: 29
Trace
#0 C:\wamp64\www\myapi\includes\DbOperations.php(15): DbOperations-
>isEmailExist('sabin@gmail.com')
#1 C:\wamp64\www\myapi\public\index.php(33): DbOperations-
>createNewUser('sabin@gmail.com', '$2y$10$j5OtIDm/...')
现在我在哪里做错了...或者我做错了,