[英]Error when trying to check if email already exists in db
I am receiving this error when I try to check if an email already exists in the database and not sure why: 当我尝试检查数据库中是否已存在电子邮件并且不确定原因时,我收到此错误:
Fatal error: Call to a member function bind_param() on a non-object in "" 致命错误:在“”中的非对象上调用成员函数bind_param()
Here is my code: 这是我的代码:
$email = $_POST['email'];
//prepare and set the query and then execute it
$stmt = $conn2->prepare("SELECT COUNT (*) FROM users WHERE email = ?");
$stmt->bind_param('s', $email);
$stmt->execute();
// grab the result
$stmt->store_result();
// get the count
$numRows = $stmt->num_rows();
if( $numRows )
{
echo "<p class='red'>Email is already registered with us</p>";
}
else
//if we have no errors, do the SQL
I have a seperate database connection file: 我有一个单独的数据库连接文件:
function DB2($host='', $user='', $password='', $db='') {
/* Create a new mysqli object with database connection parameters */
$mysqli = new mysqli($host, $user, $password, $db);
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
return $mysqli;
}
Which is linked to this file using: 使用以下链接到此文件:
$conn2 = DB2();
You didn't say what language this is in. I'm assuming it's perl and dbi. 您没有说这是什么语言。我假设它是perl和dbi。
$email = $_POST['email'];
//prepare and set the query and then execute it
$stmt = $conn2->prepare("SELECT COUNT (*) FROM users WHERE email = ?");
$stmt->bind_param($email);
$stmt->execute();
should be 应该
$email = $_POST['email'];
//prepare and set the query and then execute it
$stmt = $conn2->prepare("SELECT 1 FROM users WHERE email = ? fetch first row only");
$stmt->execute($email);
I don't know what store_result() is. 我不知道store_result()是什么。 I don't think it's part of DBI.
我不认为这是DBI的一部分。 You possibly want to do:
您可能想做:
@found = $stmt->selectrow_array();
if ($#found) { #email was found }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.