|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-08-28 05:26 UTC] talmage dot news at gmail dot com
Description: ------------ The documentation at: http://us.php.net/manual/en/mysqli.connect.php 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: --------------- <?php /* Force an error -> 3307 is not where mysql is listening */ $mysqli = new mysqli("127.0.0.1:3307", "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); exit(); } printf("Host information: %s\n", $mysqli->host_info); /* close connection */ $mysqli->close(); ?> Expected result: ---------------- Connect failed: Unknown MySQL server host '127.0.0.1:3307' Actual result: -------------- Warning: mysqli::mysqli(): (HY000/2005): Unknown MySQL server host '127.0.0.1:3307' (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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 12:00:02 2025 UTC |
Get the same problem but make investigation: $mysqli = new mysqli("localhost:3307", "my_user", "my_password", "world"); -> got error $mysqli = new mysqli("127.0.0.1:3307", "my_user", "my_password", "world"); -> got error BUT!: $mysqli = new mysqli("127.0.0.1:3306", "my_user", "my_password", "world"); -> change server port to default (3306). $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -> all works correct. Seems that variable not parsed for $HOST_NAME and $PORT_NUMBER and function try to connect using incorrect (not parsed) string.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]]]]]] ) However conn=new mysqli('localhost','user','password','my_db',3306); works as expected.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: <?php $dbObj = new mysqli( "localhost", "user", "pass", "db", 3306); echo "dbObj="; var_dump($dbObj); ?> error result: dbObj=bool(false) sucess result: dbObj==object(mysqli)#1 (0) { }Getting the same error with the following version: PHP 5.2.4-2ubuntu5.4 with Suhosin-Patch 0.9.6.2 I was attempting to suppress errors so I could control output/formatting etc. Thought i'd just re-iterate this for version related purposes. <?php //a forced username/password mismatch $dbo = @new mysqli('localhost', 'r00t', 'pass', 'my_db'); var_dump($dbo->connect_error); var_dump(mysqli_connect_error()); ?> output: Warning: main(): Couldn't fetch mysqli in /home/default/php/mysqli.php on line 4 NULL string(63) "Access denied for user 'r00t'@'localhost' (using password: YES)"