bind_param() 错误 - 在非对象上的 bind_param() 上调用方法函数


bind_param() error - call to method function on bind_param() on a non-object

我对php很陌生,这是我第一次尝试使用mysqli。我似乎无法弄清楚为什么会出现此错误?我已经审查了类似的问题,但我仍然不明白问题是什么。

这是我的代码:

<?php
require_once('abstractDAO.php');
class customerDAO extends abstractDAO {
function __construct() {
    try{
        parent::__construct();
    } catch(mysqli_sql_exception $e){
        throw $e;
    }
}
public function getCustomers(){
    //The query method returns a mysqli_result object
    $result = $this->mysqli->query('SELECT * FROM customers');
    $customers = Array();
    if($result->num_rows >= 1){
        while($row = $result->fetch_assoc()){
            $customer = new Customer($row['customerName'], $row['phoneNumber'], $row['emailAddress']);
            $customers[] = $customer;
        }
        $result->free();
        return $customers;
    }
    $result->free();
    return false;
}
/*
 * This is an example of how to use a prepared statement
 * with a select query.
 */
public function getCustomer($customerName){
    $query = 'SELECT * FROM customers WHERE customerName = ?';
    $stmt = $this->mysqli->prepare($query);
    $stmt->bind_param('s', $customerName);
    $stmt->execute();
    $result = $stmt->get_result();
    if($result->num_rows == 1){
        $temp = $result->fetch_assoc();
        $customer = new Customer($temp['customerName'], $temp['phoneNumber'], $temp['emailAddress']);
        $result->free();
        return $customer;
    }
    $result->free();
    return false;
}
public function addCustomer($customer){
    if(!$this->mysqli->connect_errno){
        $query = 'INSERT INTO customers VALUES (?,?,?)';
        $stmt = $this->mysqli->prepare($query);
        $stmt->bind_param('sss', 
                $customer->getCustomerName(), 
                $customer->getPhoneNumber(), 
                $customer->getEmailAddress());
        $stmt->execute();
        if($stmt->error){
            return $stmt->error;
        } else {
            return $customer->getCustomerName() . ' added successfully!';
        }
    } else {
        return 'Could not connect to Database.';
    }
}
}
?>

如果您需要更多代码片段,请告诉我。任何建议将不胜感激!

mysqli::p repare 如果出现错误,则返回false

false不是对象,因此您会收到错误:

call to method function on bind_param() on a non-object .

可以通过检查 $mysqli->error 属性来获取错误消息。

public function addCustomer($customer) {
    if(!$this->mysqli->connect_errno) {
        $query = 'INSERT INTO customers (customerName,phoneNumber,emailAddress) 
                                 VALUES (?,?,?)';
        $stmt = $this->mysqli->prepare($query);
        if (!$stmt) {
            $err = $this->mysqli->error;
            echo $err;
            // do something with $err
            return $err;
        }
        $stmt->bind_param('sss', 
            $customer->getCustomerName(), 
            $customer->getPhoneNumber(), 
            $customer->getEmailAddress());
        if(!$stmt->execute()){
            return $stmt->error;
        } else {
            return $customer->getCustomerName() . ' added successfully!';
        }
    } else {
        return 'Could not connect to Database.';
    }
}

prepare失败的最典型原因是格式错误或无效的查询,但在不知道客户架构或约束的情况下,我无法确定您的特定问题是什么。