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

时间:2012-02-23 17:03:56

标签: php sql pdo prepared-statement

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

致命错误:在“”

中的非对象上调用成员函数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();

1 个答案:

答案 0 :(得分:0)

你没有说出这是什么语言。我假设它是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 }