Re: [db] Catching mysql_connect errors with set_error_handler ?

From: Date: Fri, 13 Apr 2001 15:09:50 +0000
Subject: Re: [db] Catching mysql_connect errors with set_error_handler ?
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-218@lists.php.net to get a copy of this message
Markus Fischer wrote: > > 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 : I see now, yes if no valid link, mysq_errno() won't work. But there is a easy way to catch this particular error, something like: ini_set ('track_errors','1'); $con = @mysql_connect( 'localhost:/var/socken', 'user', 'pass'); if (!empty($php_errormsg) && ($con === false)) { return raiseError(DB_CONNECT_FAIL, null, null, null, $php_errormsg); } ini_restore('track_errors'); > .. 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. I think that this method is perfectly valid, and don't require too much work to implement. Cheers, Tomas V.V.Cox

« previous php.pear.dev (#218) next »