mysqli::query(): Couldn't fetch mysqli

mysqli_query(): couldn't fetch mysqli in php
mysqli select query
mysqli_real_escape_string couldn t fetch mysqli
mysqli::prepare(): couldn't fetch mysqli
php mysqli->query results
mysqli->query oop
warning mysqli close couldn t fetch mysqli in bagisto
couldn't fetch mysqli in opencart

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:

<?php
if(!isset($_SESSION)) 
{ 
    session_start(); 
}  

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

// Create new mysql connection object
$DBConnect = @new mysqli("localhost","root@localhost", 
            NULL,"Ladle");

// 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." ".
               $DBConnect->connect_error.".";
}
?>

This is my class:

 <?php 
    class EventCalendar {
        private $DBConnect = NULL;

        function __construct() {
            // Include the database connection data
            include("inc_LadleDB.php");
            $this->DBConnect = $DBConnect;  
        }

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

        function __wakeup() {
            // Include the database connection data
            include("inc_LadleDB.php");     
            $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', '".
                            $Description."')";

                // 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?

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)

[Solved] How to solve warning: mysqli::query(): couldn't fetch mysqli , How to solve warning: mysqli::query(): couldn't fetch mysqli in and notice: So I used the same code for the second button to show query result  had the same problem "mysqli::query(): Couldn't fetch mysqli". googled it and found this answer. i was closing it too late. After connecting and quering the DB i included a new php-file which didnt know the connection so it message was shown. just commenting if anyone else runs in this problem. cheers – Ferdinand Fatal Apr 11 '16 at 14:04

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

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

Solved mysqli query Couldn't fetch mysqli, Solved mysqli query Couldn't fetch mysqli. Soft Web Solution. Loading Unsubscribe from Duration: 3:02 Posted: 12 Feb 2018 Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Warning: mysqli_query(): Couldn't fetch mysqli When

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

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

Hope it helps.

php, mysqli_query(): Couldn't fetch mysqli in C:\xampp\htdocs\submit-form.php on Well, in this statement: $result = mysqli_query($connection,$query); where do  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Why Warning: mysqli_query(): Couldn't fetch mysqli?

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

Couldn't fetch mysqli - PHP, asked sitepoint a while ago. Basically I get two errors along the lines of: Couldn​'… PHP Warning: mysqli::prepare(): Couldn't fetch mysqli in  Warning: mysqli::query(): Couldn't fetch mysqli Notice: Trying to get property 'num_rows' of non-object in 0 results Warning: mysqli::close(): Couldn't fetch mysqli in It is true that similar problem have occurred and answers have published to them. I tried those and still I can't understand where my mistake is.

mysqli::close(): Couldn't fetch mysqli · Issue #429 · ThingEngineer , PHP Warning: mysqli::close(): Couldn't fetch mysqli in If you have a long running script, calling close() after sql query shouldnt cause any  mysqli_query(): Couldn't fetch mysqli stokie-ruch Hi guys I know this might be a repeated post, someone has looked at the code and they seem to think that it all look fine, but I'm getting this following message again

“Couldn't fetch mysqli” error on insert query : PHPhelp, I am receiving a "Couldn't fetch mysqli" error on the following function when trying to import data from a text file. However the insert function is working just fine in  instead of using close(); in the end of each query, use this close on the footer part of the site, so when the page loads, it will close connection, each time you switch another page, in all the way you can set an init file to make the connection again.

PHP Warning: DB\MySQLi::__construct(): Couldn , mysqli::query -- mysqli_query — Performs a query on the database The cryptic "Couldn't fetch mysqli" error message can mean any number of things,  Definition and Usage. The fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both. Note: Fieldnames returned from this function are case-sensitive.

Comments
  • Where is the line 43 that the error refers to?
  • Try to correct your mysqli object: $DBConnect = @new mysqli("localhost","root","","Ladle");
  • SOrry... try above first, you can reach mysqli object...
  • Why do you supress errors by the @ when calling new mysqli()?
  • @AycanYaşıt thank you, it worked, sorry wasn't sure how to specify the NULL password or user fields correctly
  • had the same problem "mysqli::query(): Couldn't fetch mysqli". googled it and found this answer. i was closing it too late. After connecting and quering the DB i included a new php-file which didnt know the connection so it message was shown. just commenting if anyone else runs in this problem. cheers
  • Kudos, T.T. Lesson learned: don't allow anything to destroy the DB object via a __destruct() method.
  • @T.Touda __destruct is not no fired after CLASS construction but "The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence. " See: php.net/manual/en/language.oop5.decon.php
  • Can you explain why exactly the original code is "wrong"? If the credentials were wrong (and that is the only change I see), there should be a clear error message about that
  • @NicoHaase It's simple, password for root user is empty string, and NULL is provided. When credentials are incorrect, error message is generally like given error at top of the question.
  • @AycanYaşıt but in which case would you get the error message "Couldn't fetch mysqli" when providing wrong credentials? Additionally, the documentation states that when using no argument, a user without a password is selected
  • worked for me, dolibarr 12.0.0-alpha. Thanks
  • Can you explain further how that connects? There should be another very clear error message if something would be wrong with the database name, and both types of dashes are allowed to be used in a database name