Why am I getting a \'Call to a Member Function prepare() on a Non-Object\' error in PHP?

Patricia Arquette
Release: 2024-11-25 20:07:12
Original
505 people have browsed it

Why am I getting a

Call to a Member Function Prepare() on a Non-Object Error in PHP

In PHP, the "Call to a member function prepare() on a non-object" error occurs when you attempt to call the prepare() method on a variable that is not an object. This typically happens when the variable does not refer to an instance of the mysqli class or when the mysqli object has not been initialized properly.

Understanding the Issue

In your provided code, the $DBH variable is not initialized as an object of the mysqli class when you call the selectInfo() function. This is why you are encountering the error. To resolve this issue, you can use any of the following approaches:

Approach 1: Use Global Scope

global $DBH;
function selectInfo($limit, $offset) {
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
}
Copy after login

This approach declares $DBH as a global variable, allowing you to access it within the selectInfo() function. However, it's not considered best practice as it can lead to global variable pollution.

Approach 2: Pass the Object as a Parameter

function selectInfo($DBH, $limit, $offset) {
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
}
selectInfo($DBH, 10, 20);
Copy after login

This approach directly passes the mysqli object as a parameter to the selectInfo() function. It is more explicit and ensures that the object is properly initialized.

Approach 3: Use a Database Helper Function

function getDBH() {
    static $DBH = null;
    if (is_null($DBH)) {
        $DBH = new mysqli("host", "test", "123456", "dbname");
    }
    return $DBH;
}

function selectInfo($limit, $offset) {
    $DBH = getDBH();
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
}
Copy after login

This approach uses a helper function to retrieve the mysqli object and initialize it if necessary. It ensures that the object is always available when calling selectInfo().

Approach 4: Use a Database Class

class Database {
    private $DBH;

    public function __construct($host, $user, $pass, $dbname) {
        $this->DBH = new mysqli($host, $user, $pass, $dbname);
    }

    public function prepare($sql) {
        return $this->DBH->prepare($sql);
    }
}

$db = new Database("host", "test", "123456", "dbname");
$stmt = $db->prepare("SELECT * FROM information LIMIT ?,?");
Copy after login

This approach encapsulates the database connection within a class. It provides a clean and structured way to access and prepare statements.

The above is the detailed content of Why am I getting a \'Call to a Member Function prepare() on a Non-Object\' error in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template