I see in the PHP 4.0.6 release notes that mysql_error() and mysql_errno()
have been fixed so that you can get error information from them when
a connect call fails. (Previously they worked only after successfully
establishing a connection, so that you had to use $php_errormsg with
track_errors turned on to get connection error information.)

I want to write connection code that takes advantage of this, so that
it uses the MySQL error functions if they're the fixed versions, and
falls back to $php_errormsg otherwise.  Does the following look reasonable?

if (!($conn_id = @mysql_connect ($host, $user, $password)))
     # If mysql_errno()/mysql_error() work for failed connections, use
     # them (invoke with no arguments). Otherwise, use $php_errormsg.
     if (mysql_errno ())
         die (sprintf ("Cannot connect to database server: %s (%d)\n",
                 htmlspecialchars (mysql_error ()),
                 mysql_errno ()));
         die ("Cannot connect to database server: "
                 . htmlspecialchars ($php_errormsg) . "\n");

I'm assuming that the proper way to invoke mysql_error()/mysql_errno()
after a failed connection is with *no* argument, because when a failure
occurs $conn_id won't have any reasonable value and shouldn't be passed
to those functions.


PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to