|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45940 MySQLI OO does not populate connect_error property on failed connect
Submitted: 2008-08-28 05:26 UTC Modified: 2009-03-13 22:55 UTC
Avg. Score:4.1 ± 1.1
Reproduced:16 of 17 (94.1%)
Same Version:10 (62.5%)
Same OS:5 (31.2%)
From: talmage dot news at gmail dot com Assigned: mysql
Status: Closed Package: MySQLi related
PHP Version: 5.2.6 OS: Centos 5
Private report: No CVE-ID:
 [2008-08-28 05:26 UTC] talmage dot news at gmail dot com
The documentation at:

Provides an example for the OO method of constructing a MySQLI instance and checking for error.

However, when testing this example and forcing an error the output is not as the documentation describes (which as documented is the way I would expect the OO library to behave.)

I first noticed this behavior on a PHP 5.2.3 install on my Centos server. I then tried the same thing on a Debian box on 5.2.3 and received the same unexpected behavior.

I then downloaded and compiled PHP 5.2.6 on my Centos box and still received the unexpected behavior.

Reproduce code:

/* Force an error -> 3307 is not where mysql is listening */
$mysqli = new mysqli("", "my_user", "my_password", "world");

/* Check for error: This should return true and enter the if block */
if ($mysqli->connect_error) {
    printf("Connect failed: %s\n", $mysqli->connect_error);

printf("Host information: %s\n", $mysqli->host_info);

/* close connection */

Expected result:
Connect failed: Unknown MySQL server host ''

Actual result:
Warning: mysqli::mysqli(): (HY000/2005): Unknown MySQL server host '' (3) in /srv/php/projects/test/src/test.php on line 4

Warning: main(): Couldn't fetch mysqli in /srv/php/projects/test/src/test.php on line 7

Warning: main(): Couldn't fetch mysqli in /srv/php/projects/test/src/test.php on line 12
Host information: 

Warning: mysqli::close(): Couldn't fetch mysqli in /srv/php/projects/test/src/test.php on line 15


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-29 03:59 UTC] talmage dot news at gmail dot com
Upgrading to snap did not fix.

[root@devel1 src]# /usr/local/php/bin/php -v
PHP 5.2.7-dev (cli) (built: Aug 28 2008 20:54:37) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
[root@devel1 src]# /usr/local/php/bin/php -q test.php

Warning: mysqli::mysqli(): (HY000/2005): Unknown MySQL server host '' (3) in /srv/php/projects/test/src/test.php on line 4

Warning: main(): Couldn't fetch mysqli in /srv/php/projects/test/src/test.php on line 7

Warning: main(): Couldn't fetch mysqli in /srv/php/projects/test/src/test.php on line 12
Host information: 

Warning: mysqli::close(): Couldn't fetch mysqli in /srv/php/projects/test/src/test.php on line 15
[root@devel1 src]#
 [2008-08-29 11:51 UTC]
I can reproduce that, assigning to mysql folks.
 [2008-11-29 00:08 UTC] mudroeb at gmail dot com
Get the same problem but make investigation:

$mysqli = new mysqli("localhost:3307", "my_user", "my_password",
-> got error

$mysqli = new mysqli("", "my_user", "my_password",
-> got error

$mysqli = new mysqli("", "my_user", "my_password",
-> change server port to default (3306).

$mysqli = new mysqli("localhost", "my_user", "my_password",
-> all works correct. 
Seems that variable not parsed for $HOST_NAME and $PORT_NUMBER and function try to connect using incorrect (not parsed) string.
 [2008-11-29 00:22 UTC] valentin71 at gmail dot com
Not quite sure that mudroeb is right.  
According to manual the right call format must be following:
mysqli mysqli_connect ( [string host [, string username [, string passwd [, string dbname [, int port [, string socket]]]]]] )

conn=new mysqli('localhost','user','password','my_db',3306);
works as expected.
 [2008-12-01 13:17 UTC] dannyrjohnston at msn dot com
I have duplicated this issue.

According to the manual mysqli::__construct should always return a object, but the function is returning a false value on error.

I have tried three different possible error conditions and have the following results:

   Bad user/password combo: mysqli_connect_error() returns "Access denied for user 'xxx'@'xxx' (using password: YES)", return value is false.

   User does not have access to the db selected: mysqli_connect_error()returns "Access denied for user 'xxx'@'xxx' to database 'xxx'", return value is false.

   Bad port number: mysqli_connect_error() returns "Can't connect to MySQL server on 'localhost' (10061)", return value is false.

I have not checked any other cases such as bad socked name, but in each of the above conditions I received a false value not an object.

test code:
    $dbObj = new mysqli( "localhost", "user", "pass", "db", 3306);

     echo "dbObj=";

error result:

sucess result:
dbObj==object(mysqli)#1 (0) {
 [2008-12-01 13:39 UTC] dannyrjohnston at msn dot com
Forget my previous comment.  I found some code between the new mysqli statement in the example I was using which was setting the $dbObj to false if mysql_connect_error was returning a value.
 [2008-12-25 21:17 UTC] ws at develtheory dot com
Was able to reproduce the error in the same method on php 5.2.8, both with purposeful incorrect passwords and nonexistent host/port combos.

the object is created, connect_error returns NULL on var_dump, error log dumps:
"Dec 25 15:12:18 dev01 httpd: PHP Warning:  dbo::__construct() [<a href='dbo.--construct'>dbo.--construct</a>]: Couldn't fetch mysqli in /nfs/beta/www/classes/ on line 88"
 [2009-01-10 10:23 UTC] nullhility at gmail dot com
Getting the same error with the following version:
PHP 5.2.4-2ubuntu5.4 with Suhosin-Patch

I was attempting to suppress errors so I could control output/formatting etc. Thought i'd just re-iterate this for version related purposes.

//a forced username/password mismatch
$dbo = @new mysqli('localhost', 'r00t', 'pass', 'my_db');



Warning: main(): Couldn't fetch mysqli in /home/default/php/mysqli.php on line 4
string(63) "Access denied for user 'r00t'@'localhost' (using password: YES)"
 [2009-01-12 12:56 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

 [2009-02-17 10:41 UTC]
Fixed in 5.2 CVS, too, now.
 [2009-03-13 22:55 UTC]
Added information to the documentation.
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Thu Apr 17 18:02:13 2014 UTC