php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69884 mysqli->connect_error seems to be shared across objects
Submitted: 2015-06-19 14:19 UTC Modified: 2021-06-09 16:31 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (33.3%)
From: phpbugs at olemartin dot org Assigned:
Status: Re-Opened Package: MySQLi related
PHP Version: 5.5.26 OS: Ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpbugs at olemartin dot org
New email:
PHP Version: OS:

 

 [2015-06-19 14:19 UTC] phpbugs at olemartin dot org
Description:
------------
It seems that the \mysqli->connect_error and \mysqli->connect_errno values (when using mysqli in the object-oriented mode) shares state between all connections. This is not documented (it should at least have a warning in the documentation).

When running the example code (make sure you point the first and last connection to a valid MySQL server) i found that all connections reported no errors, although the second one failed. Sometimes (although I'm having a hard time reliably reproducing this) i'm pretty sure i've also seen all three connections report errors (although the third and last connection succeeded). Maybe this value is not overwritten when a successful connection is done, in all code paths?

Either way, i think this should be either documented or fixed - as one would not expect this information to be shared between instances when the syntax doesn't indicate that. If this is a static property, i would expect the documentation said to use \mysqli::$connect_error, but that doesn't even work.   

Test script:
---------------
$connections = array(
    'Valid connection' =>   new mysqli('127.0.0.1', 'root', '', 'test', 3306),
    'Invalid connection' => @new mysqli('127.0.0.1', 'root', '', 'test', 3307),
    'Valid connection 2' => new mysqli('127.0.0.1', 'root', '', 'test', 3306),
);

foreach($connections as $desc => $conn) {
    echo "$desc:\n".
        "connect_error = ".$conn->connect_error."\n".
        "connect_errno = ".$conn->connect_errno."\n".
        "ping =          ".@$conn->ping()."\n\n".
}


Expected result:
----------------
Valid connection:
connect_error =
connect_errno = 0
ping =          1

Invalid connection:
connect_error = Connection refused
connect_errno = 2002
ping =          

Valid connection 2:
connect_error = 
connect_errno = 0
ping =          1

Actual result:
--------------
Valid connection:
connect_error = 
connect_errno = 0
ping =          1

Invalid connection:
connect_error = 
connect_errno = 0
ping =             

Valid connection 2:
connect_error = 
connect_errno = 0
ping =          1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-13 21:14 UTC] brak at gameservers dot com
I'm also seeing this same behavior on PHP 7.1.19 running on CentOS 7
 [2018-08-14 02:54 UTC] requinix@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2018-08-14 02:54 UTC] requinix@php.net
The values are indeed "shared", meaning the values are not associated with a specific instance but set globally.
 [2020-10-19 09:58 UTC] call dot user dot func at gmail dot com
Hi, this behaviour is the same in php8+. A warning in the docs would've been really helpful, as it's a pain to debug.

A very brief testcase:
$db1 = new MySQLi("localhost", "root", "", "mysql", 3306);
var_dump("1: ".$db1->connect_error); // not error, db exist

$db2 = new MySQLi("localhost", "root", "", "some_non_existing_db", 3306);
var_dump("2: ".$db2->connect_error); // error, db not exist

// db2 shared error to db1 which is wrong, db1 is still valid
var_dump("3: ".$db1->connect_error); // not error, db exist
 [2021-02-12 12:09 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2021-02-12 12:09 UTC] cmb@php.net
These are effectively static properties.
 [2021-02-12 12:09 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=efaedacb533200d9007d704d07e624b27c946036
Log: Fix #69884: mysqli->connect_error seems to be shared across objects
 [2021-02-12 12:09 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2021-02-13 09:34 UTC] mumumu@php.net
Automatic comment on behalf of mumumu@mumumu.org
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=b3ef2c18bac3cea8e6d0493723e54f7e2a92ab11
Log: Fix #69884: mysqli->connect_error seems to be shared across objects
 [2021-06-09 16:31 UTC] cmb@php.net
-Status: Closed +Status: Re-Opened -Assigned To: cmb +Assigned To:
 [2021-06-09 16:31 UTC] cmb@php.net
My fix was nonsense, so I reverted.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC