0

I am very new to php and this is my first attempt at using mysqli. I can't seem to figure out why I am getting this error? I have reviewed similar questions on it but I still don't understand what the problem is.

Here is my code:

<?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.';
    }
}

}

?>

Let me know if you need any more code snippets. Any suggestions would be very much appreciated!

10
  • Are you getting the error in addCustomer, getCustomer or both? Does get getCustomers work? Commented Nov 15, 2014 at 15:48
  • Here is the error message: Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\Week11Lab\customerDAO.php on line 59 Commented Nov 17, 2014 at 17:07
  • I'm getting the error in addCustomer. I don't know if getCustomers works as I can't add an initial customer. Commented Nov 17, 2014 at 17:35
  • have a look at my answer and try the $mysqli->error property what does it say? Also change your insert statement so that it matches mine. Commented Nov 17, 2014 at 17:48
  • I get the following messages: Notice: Undefined variable: mysqli in C:\xampp\htdocs\Week11Lab\customerDAO.php on line 57 Commented Nov 17, 2014 at 17:54

1 Answer 1

1

mysqli::prepare returns false if there was an error.

false is not an object, thus you get the error:

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

You can get the error message by examining the $mysqli->error property.

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

The most typical reason why prepare fails is a malformed or invalid query, but without knowing the customer schema or constraints I can't be sure what your particular problem is.

Sign up to request clarification or add additional context in comments.

4 Comments

@macattack Please see my updated answer, it focuses on the addCustomer method. You should be able to copy and paste it directly into your customerDAO class
Thanks that's helping with the debugging. Still not there though!
I have this error now which is at bind_param: Fatal error: Call to undefined method Customer::getCustomerName() in C:\xampp\htdocs\Week11Lab\customerDAO.php on line 92
@macattack Your customer class does not have a method called getCustomerName. In either case your DB issue looks to be solved. Please consider up voting and accepting my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.