Re: [db] Catching mysql_connect errors with set_error_handler ?
| From: | Markus Fischer | Date: | Fri, 13 Apr 2001 14:06:16 +0000 |
| Subject: | Re: [db] Catching mysql_connect errors with set_error_handler ? | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-217@lists.php.net to get a copy of this message | ||
Hello Tomas,
I hope I didn't get it wrong, but I think there was a little
missunderstandig.
If I don't use 'set_error_handler' before calling mysql_connect,
there is no way to get the exact error from withing PHP. So,
regardless of any modifications I do to the DB/mysql.php you
suggested, I can't catch the right error message.
Here is an example not using pear :
<?
// in this example, the path to the unix socket is wrong,
// we assume username and password are right
// (but we don't need to care either since the socket is
// wrong)
$h = mysql_connect( 'localhost:/var/socken', 'user', 'pass');
// show us $h, its obviously 'false'
var_dump( $h);
// lets get the error message
// there is no difference if you supply the parameter
// here or not since its false; you would only get the error
// message its not a valid mysql link
echo 'error = '.mysql_errno().', msg = '.mysql_error()."\n";
?>
php's output (formatted for better readability|) :
Warning: MySQL Connection Failed: Can't connect to local
MySQL server through socket '/var/socke' (111)
in mysqltest.php on line 2
bool(false)
error = , msg =
The only way to catch this is to use set_error_handler :
<?
// Catch all errors with mysql_errorhandler();
set_error_handler( 'mysql_errorhandler');
// since we are catching the error on our own
// there is no need for '@' either
$h = mysql_connect( 'localhost:/var/socken', 'user', 'pass');
// restore the old handler since from now on we can
// use mysql_errno() and friends
restore_error_handler();
function mysql_errorhandler( $code, $message) {
echo "Catched error code : $code\nMessage : $message\n\n";
}
?>
This would yield the following output :
Catched error code : 2
Message : MySQL Connection Failed: Can't connect to local MySQL
server through socket '/var/socken' (111)
The benefit is to programmatically catch the error message so the
user can retrieve it with the appropriate PEAR/DB function.
I hope I could explain this so everyone could follow me.
so.
Your solution was :
> The solution is easy. Something like change the mysqlRaiseError of
> mysql.php with:
>
> function mysqlRaiseError($errno = null)
> {
> if ($errno === null) {
> $errno = mysql_errno($this->connection);
> }
> return $this->raiseError($this->errorCode($errno), null, null,
> null, mysql_error($this->connection));
> }
.. to use mysql_error($this->connection)); But as you can see in
the simple example above, this would just return nothing.
Changing the default behaviour of the mysql extension is not
trivial and will probably never happen.
kind regards,
Markus
--
Markus Fischer, http://josefine.ben.tuwien.ac.at/~mfischer/
EMail: mfischer@josefine.ben.tuwien.ac.at
PGP Public Key: http://josefine.ben.tuwien.ac.at/~mfischer/C2272BD0.asc
PGP Fingerprint: D3B0 DD4F E12B F911 3CE1 C2B5 D674 B445 C227 2BD0