Re: [db] Catching mysql_connect errors with set_error_handler ?
| From: | Tomas V.V.Cox | Date: | Fri, 13 Apr 2001 00:12:58 +0000 |
| Subject: | Re: [db] Catching mysql_connect errors with set_error_handler ? | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-216@lists.php.net to get a copy of this message | ||
Markus Fischer wrote:
>
> Hello,
>
> While writing an application for accessing different
> databases using PEAR/DB, I noticed that I was not able to
> catch the exact error when I e.g. used the wrong socket path
> for the connection. When using a wrong username or some other
> wrong information, the feedback from pear/db is only
> 'DB Error: connect failed'.
>
> While pear/db reports only 'DB Error: connect failed' you can,
> using set_error_handler before calling mysql_connect,
> catch the exact error like :
>
> MySQL Connection Failed: Unknown MySQL Server Host 'localhostf'
> or
> MySQL Connection Failed: Can't connect to local MySQL server through socket
> '/var/ruqld/mysqld.sock'
> or
> MySQL Connection Failed: Access denied for user: 'rooter@localhost'
> or
> MySQL Connection Failed: Can't connect to MySQL server on '127.0.0.1'
>
> I'm not deeply involved in pear coding and its standards,
> but can there be seen a possibility to implement this ?
>
> Currently I can't verify if this is the same for other database
> but I won't exclude it.
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));
}
The last param fills the userinfo property of a Pear_Error obj (this is
yet implemented in ifx.php and pgsql.php). So you can view the native
error so:
if (DB::isError($con = DB::connect($dsn)) {
die ($con->getDebugInfo()); // or better: getUserInfo()
}
Hope that helps,
Tomas V.V.Cox