php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40588 mysqli_connect_error() == "" on error
Submitted: 2007-02-22 04:45 UTC Modified: 2007-02-22 22:18 UTC
From: frankpw at fw2s dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.2.1 OS: Win XP Pro
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: frankpw at fw2s dot com
New email:
PHP Version: OS:

 

 [2007-02-22 04:45 UTC] frankpw at fw2s dot com
Description:
------------
In my class I'm opening a connection with mysqli_connect(). mysqli_connect_error() returns description of any error except one. If one parameter is of wrong type (i.e port as string rather than numeric). 

Reproduce code:
---------------
  public function OpenConnection($host, $user, $pass, $name = null, $port = null, $sock = null, $chrs = null)
  {
    $this->mysqli = @mysqli_connect($host, $user, $pass, $name, $port, $sock);
    if (empty($this->mysqli)) die ("Execution stopped! " . mysqli_connect_error() . "<br />\n");
    if (!empty($chrs)) $this->DefaultCharacterSet($chrs);
  }


Expected result:
----------------
"Execution stopped! mysqli_connect expects parameter 5 to be long, string given"

Actual result:
--------------
"Execution stopped!"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-22 10:59 UTC] tony2001@php.net
Cannot reproduce.
Make sure the client library is of the same version is the MySQL server.
 [2007-02-22 18:34 UTC] frankpw at fw2s dot com
Additional info:
Server info: 5.0.27-community-max-nt
Client info: 5.0.22
That's as close to match server and client versions as it gets. I've downloaded php_mysql.dll and php_mysqli.dll for server version 5.0.27 directly from MySQL. Please try the code below:
<?php
$mysqli = @mysqli_connect("localhost","root","pass",null,"abcd");
if (empty($mysqli)) die ("Connection attempt failed! " . mysqli_connect_error() . "<br />\n");
echo "Connected!<br />\n";
echo "Host info: " . mysqli_get_host_info($mysqli) . "<br />\n";
echo "Client info: " . mysqli_get_client_info() . "<br />\n";
echo "Server info: " . mysqli_get_server_info($mysqli) . "<br />\n";
echo "Protocol version: " . mysqli_get_proto_info($mysqli) . "<br />\n";
?>
For test purposes make sure that first four parameters are correct and use fifth as is. Repeat the test with "@" removed from the call to mysqli_connect(). You'll see that warning is being displayed but mysqli_connect_error() is empty and mysqli_connect_errno() == 0.
 [2007-02-22 19:02 UTC] tony2001@php.net
Remove the @, you'll see the error.
 [2007-02-22 19:42 UTC] frankpw at fw2s dot com
That is not a point. Even the manual shows how connection errors should be handled:
/* check connection */ 
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}
This check fails in this particular case because mysqli_connect_error() is empty and mysqli_connect_errno is 0. Are you insisting that this is not connection error and that it shoud be handled differently?
 [2007-02-22 19:55 UTC] tony2001@php.net
>This check fails in this particular case 

This check should be used when you pass correct parameters to the function.

>Are you insisting that this is not connection error and 
>that it shoud be handled differently?

Yes, I do.
You're passing wrong parameters to the function. 
 [2007-02-22 20:19 UTC] frankpw at fw2s dot com
I promise to end this discussion if you tell me that passing port number as "3306" or "10" (notice quotes) is correct. Port is declared as int not a string. With above values "3306" will make connection (assuming that server is set to listen on port 3306) and with "10" will fail with proper values returned by mysqli_connect_errno and mysqli_connect_error. Isn't it logical to assume that if connection fails (for whatever reason) this reason should be consistently stated in responses from mysqli_connect_errno() and mysqli_connect_error()?
 [2007-02-22 20:27 UTC] tony2001@php.net
That's easy - "3306" and "10" are numeric strings, but "abcd" is not.

>Isn't it logical to assume that if connection fails
That's the point. Connection doesn't fail. 
The function is called with wrong parameters, so it doesn't even _try_ to connect, because we already know the parameters are wrong.
You can see the same with mysqli_connect() without any parameters.
 [2007-02-22 22:18 UTC] frankpw at fw2s dot com
As promised I'm not attempting to continue, therfore I didn't change status to open. And you don't have to answer this. It would be much easier for me to accept a statement: "I like it the way it is so don't bother to report this as an error.". It doesn't matter if mysqli_connect() attempts a connection or not. This attempt is made by a developer, programmer or whoever is using this function, and, according to documentation, expecting to get a promised response - if connection was made then no error, if connection wasn't made, then error is being reported, like any other. If your arguments are valid then why do we need error handling at all. I think we are from totally different schools of logic. Your logic allows three values: false, true, and something in between.

Best regards,

Frank P. Walentynowicz
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 04:01:27 2024 UTC