|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33635 "couldn't fetch mysqli" error when attempting to write session data to db
Submitted: 2005-07-10 21:54 UTC Modified: 2005-07-19 21:45 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: tony at marston-home dot demon dot co dot uk Assigned: georg (profile)
Status: Not a bug Package: MySQLi related
PHP Version: 5CVS-2005-07-10 (dev) OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: tony at marston-home dot demon dot co dot uk
New email:
PHP Version: OS:


 [2005-07-10 21:54 UTC] tony at marston-home dot demon dot co dot uk
In my script I use a custom session handler to write all session data to the database. Through versions 5.0.0 to 5.0.4 this worked as expected, but with 5.0.5dev I am now getting an error. It only ever failos at the end of the script when writing out the session data. It always fails at the same place by generating a "Couldn't fetch mysqli" error.

Reproduce code:
function connect ($dbname)
// establish a connection to the database
    global $dbhost, $dbusername, $dbuserpass;

    static $dbconn;
    if (!$dbconn) {
        $dbconn = mysqli_connect($dbhost, $dbusername, $dbuserpass);
        if ($dbconn) {
            // use UTF8 for this connection
            $this->query = "SET names 'UTF8'";
            $result = mysqli_query($dbconn, $this->query) or trigger_error($this, E_USER_ERROR);
        } // if
    } // if
    if (!$dbconn) {    <== FAILS HERE *********************
        return false;
    } elseif (!mysqli_select_db($dbconn, $dbname)) {
        return false;
    } else {
        $this->dbconnect = $dbconn;
        return true;
    } // if
} // connect

The full code is available from The script to run is "test.php".

Expected result:
At that point $dbconn is shown to be "object of mysqli", so it should not error.

Actual result:
At the line "if (!$dbconn)" the program jumps immediately to my error handler with the error "Couldn't fetch mysqli".


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-10 22:11 UTC] tony at marston-home dot demon dot co dot uk
The full test set can be downloaded from
 [2005-07-10 22:23 UTC]
Please, provide a *short* but *complete* reproduce case without all those numerous classes, includes etc. Thanks.
 [2005-07-10 23:44 UTC] tony at marston-home dot demon dot co dot uk
It simply does not fail when I remove all those classes. That is the smallest I can possibly make it, and I have been working on it all afternoon.
 [2005-07-10 23:59 UTC]
"Smallest possible" doesn't mean that you remove everything.
Or does it stop failing when you remove just ONE class?

 [2005-07-11 00:12 UTC] tony at marston-home dot demon dot co dot uk
php_session class is a subclass of std.table class, so i suppose they could be combined, but it still wouldn't reduce the total code. It is the interaction between the table class and the mysql class which appears to be causing the problem.

The point is that the line where it fails is executed without error several times before the failure occurs. If I remove the custom session handler the code never fails. It does not fail when reading the session data, only when writing.
 [2005-07-11 09:09 UTC]
1. Put everything in single file
2. Remove the PHP4 stuff (in PHP5, you don't need to do =& with objects, for example)
3. Remove all unused methods

 [2005-07-11 11:59 UTC] tony at marston-home dot demon dot co dot uk
I have changed the test set as follows:
(a) Everything is in a single file (test.php)
(b) I have removed the PHP 4 code (=& with objects)
(c) I have removed redundant methods.

New test set is available at
 [2005-07-11 12:33 UTC] tony at marston-home dot demon dot co dot uk
Whoops. That URL should be
 [2005-07-11 22:48 UTC]
Please read the friendly manual about Mysqli. You're trying to mix both OO and procedural way of accessing the DB. Choose one  and stick to that. (And I don't get the idea of making the connection 'static' either..especially if you deal with OOP)

 [2005-07-11 23:55 UTC] tony at marston-home dot demon dot co dot uk
You are completely mistaken. I am not mixing both OO and procedural ways of accessing the DB - all my calls to the mysqli_* functions are in the procedural style. The fact that I am making these calls inside my own OO class should be totally irrelevant.

The same code has worked perfectly through php 5.0.0 to 5.0.4, and with 5.0.5-dev only fails when I try to write my session data to the database. SOMETHING HAS BEEN INTRODUCED IN 5.0.5-dev THAT CAUSES THIS FAILURE.

My use of static variables follows the documentation for the singleton pattern at It allows to to call mysqli_connect() only once for a request.
 [2005-07-12 01:08 UTC]
Assigned to the author of the extension, who can propably explain this better..

 [2005-07-15 09:41 UTC] tony at marston-home dot demon dot co dot uk
This problem only occurs when writing out session data to a database when the script terminates, and also because I am using a shared database connection (the one created when the session data was forst read in).

I conducted a little experiment by replacing the exit function with session_write_close(), and this did not produce the error. I therefore strongly suspect that the order of events at script termination has been altered to:
(a) close all resources
(b) write session data (to database)

This will fail if (b) uses a resource closed in (a)
 [2005-07-19 21:45 UTC]
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See bug #33772 which has a nice and short script that demonstrates the same issue. (it's not Mysqli only issue after all)

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Mon Dec 11 09:01:31 2023 UTC