繁体   English   中英

尝试检查数据库中是否已存在电子邮件时出错

[英]Error when trying to check if email already exists in db

当我尝试检查数据库中是否已存在电子邮件并且不确定原因时,我收到此错误:

致命错误:在“”中的非对象上调用成员函数bind_param()

这是我的代码:

$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

我有一个单独的数据库连接文件:

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;
  }

使用以下链接到此文件:

$conn2 = DB2();

您没有说这是什么语言。我假设它是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();

应该

$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);

我不知道store_result()是什么。 我不认为这是DBI的一部分。 您可能想做:

@found = $stmt->selectrow_array();
if ($#found) { #email was found }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM