Home > Backend Development > PHP Tutorial > Why is my mysqli::query() failing with a 'Couldn't fetch mysqli' Database Connection Error?

Why is my mysqli::query() failing with a 'Couldn't fetch mysqli' Database Connection Error?

Susan Sarandon
Release: 2024-12-07 21:05:14
Original
782 people have browsed it

Why is my mysqli::query() failing with a

Mysqli::query(): Database Connection Error

The "mysqli::query(): Couldn't fetch mysqli" error typically indicates a problem with the database connection. The following steps describe the issue and provide a solution based on the provided code snippet:

  1. Connection: In the connection file, the @ symbol in new mysqli() is used to suppress error reporting. While this is meant to prevent error display, it's better to handle errors properly instead of ignoring them.
  2. Closing: In the class constructor, you assign $DBConnect to a class member variable. This ensures that the connection object is available throughout the class. However, in the __destruct() method, you close the connection if there is no connection error. This is an issue because subsequent queries would fail as the connection is no longer available.
  3. Reopening: The __wakeup() method is intended to reopen the connection after serialization. However, it is not invoked when the class is instantiated, which means that the connection remains closed.

Solution:

  1. Remove the @ symbol from the connection object instantiation. Handle errors in a more robust manner.
  2. Move the connection closing code to a separate method and call it only when it's necessary to close the connection.
  3. Ensure that the __wakeup() method is invoked when the class is instantiated (e.g., when retrieving an object from a session).

Modified Code:

// Connection file
$DBConnect = new mysqli("localhost", "root@localhost", NULL, "Ladle");

// Check for connection error
if ($DBConnect->connect_errno) {
    $ErrorMsgs[] = "The database server is not available. Connect Error is " . $DBConnect->connect_errno . " " . $DBConnect->connect_error . ".";
}

// Class
class EventCalendar {
    private $DBConnect = NULL;

    function __construct() {
        include("inc_LadleDB.php");
        $this->DBConnect = $DBConnect;
    }

    function __destruct() {
        // Close the connection only if it's not closed already
        if (!$this->DBConnect->connect_error) {
            $this->DBConnect->close();
        }
    }

    function __wakeup() {
        // Include the database connection data
        include("inc_LadleDB.php");
        $this->DBConnect = $DBConnect;
    }
    
    // Event adding method
    // ...
}
Copy after login

The above is the detailed content of Why is my mysqli::query() failing with a 'Couldn't fetch mysqli' Database Connection Error?. 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