Warning: mysqli::query(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\my portable files\class_EventCalendar.php on line 43

The following is my connection file:


// Create array to hold error messages (if any)
$ErrorMsgs = array();

// Create new mysql connection object
$DBConnect = @new mysqli("localhost","[email protected]", 

// Check to see if connection errno data member is not 0 (indicating an error)
if ($DBConnect->connect_errno) {

    // Add error to errors array
    $ErrorMsgs[]="The database server is not available.".
               " Connect Error is ".$DBConnect->connect_errno." ".

This is my class:

    class EventCalendar {
        private $DBConnect = NULL;

        function __construct() {
            // Include the database connection data
            $this->DBConnect = $DBConnect;  

        function __destruct() {
            if (!$this->DBConnect->connect_error) {

        function __wakeup() {
            // Include the database connection data
            $this->DBConnect = $DBConnect;

        // Function to add events to Zodiac calendar
        public function addEvent($Date, $Title, $Description) {
            // Check to see if the required fields of Date and Title have been entered
            if ((!empty($Date)) && (!empty($Title))) {
                /* if all fields are complete then they are 
                   inserted into the Zodiac event_calendar table */
                $SQLString = "INSERT INTO tblSignUps".
                           " (EventDate, Title, Description) ".
                           " VALUES('$Date', '$Title', '".

                // Store query results in a variable
                $QueryResult = $this->DBConnect->query($SQLString);

I'm not great with OOP PHP and I'm not sure why this error is being raised. I pulled this code from elsewhere and the only thing I changed was the @new mysqli parameters. Can anyone help me to understand what is going wrong?

Solution 1

Probably somewhere you have DBconnection->close(); and then some queries try to execute .

Hint: It's sometimes mistake to insert ...->close(); in __destruct() (because __destruct is event, after which there will be a need for execution of queries)

Solution 2

Reason of the error is wrong initialization of the mysqli object. True construction would be like this:

$DBConnect = new mysqli("localhost","root","","Ladle");

Solution 3

I had the same problem. I changed the localhost parameter in the mysqli object to '' instead of writing 'localhost'. It worked; Im not sure how or why.

$db_connection = new mysqli("","root","","db_name");

Solution 4

Check if db name do not have "_" or "-" that helps in my case