tags:

views:

29

answers:

2

Hello.

Can i ask a fundamental question. Why when I try to create a new mysqli object in php with invalid database infomation (say an incorrect database name) does it not return an error intstantly? I usually program server stuff in Java and something like this would throw back an error strait away, not after 20 seconds or so.

For example

$conn = new mysqli($host, $username,    $password, $database);

Thanks!

+2  A: 

If the host, username, and password are all correct, an incorrect database name will return immediately. However, if you have the host name incorrect, the connect attempt will first have to timeout. Incorrect username and password errors also are returned immediately.

Gary
thanks for that. The host name is also incorrect. Thanks for helping me understand a little more :)
Dori
A: 

That is because the request is invalid, as an important credential is missing or is incorrect; or that the database is not accessible.

If the host name, username and/or password is incorrect, the SQL request will have to timeout before it can return. The standard timeout time in PHP is set to, I believe, 20 seconds, so it'll match very well to what you are experiencing. Though it will not happen with an invalid database name, as that would just make the request fail immediately.

To check for an error, you can call mysql_error() to get the specification of the error from PHP, or mysql_errno() to get just the error code. The standard timeout time can be set to a lower value by editing the php.ini file (if you have access to it); set the value followed by mysql.connect_timeout (that is, in php.ini) to whatever timeout time you'd want, in seconds. In normal situations, a timeout time of 5 seconds or more is good, perhaps even unnecessarily big.

Sune Rasmussen