Bug->Doc #69884 [Opn->Ver]: mysqli->connect_error seems to be shared across objects

From: Date: Tue, 14 Aug 2018 02:54:13 +0000
Subject: Bug->Doc #69884 [Opn->Ver]: mysqli->connect_error seems to be shared across objects
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-15957@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69884&edit=1

 ID:                 69884
 Updated by:         requinix@php.net
 Reported by:        phpbugs at olemartin dot org
 Summary:            mysqli->connect_error seems to be shared across
                     objects
-Status:             Open
+Status:             Verified
-Type:               Bug
+Type:               Documentation Problem
 Package:            MySQLi related
 Operating System:   Ubuntu
 PHP Version:        5.5.26
 Block user comment: N
 Private report:     N

 New Comment:

The values are indeed "shared", meaning the values are not associated with a specific
instance but set globally.


Previous Comments:
------------------------------------------------------------------------
[2018-08-13 21:14:30] brak at gameservers dot com

I'm also seeing this same behavior on PHP 7.1.19 running on CentOS 7

------------------------------------------------------------------------
[2015-06-19 14:19:44] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=69884&edit=1


Thread (5 messages)

« previous php.doc.bugs (#15957) next »