Page 1 of 1

How to capture exceptional error when connect to Mysql DB

Posted: Mon Jan 29, 2007 10:01 pm
by vin_hew
Hi All,

I'm new in PHP and MySql, I did Ms VS .Net programming as well. Just wonder how am I going to

write PHP code to capture the expectional error during connecting to MySQL database. In .Net

there is something like try and catch?

Thanks

Cheers!

Posted: Mon Jan 29, 2007 10:16 pm
by feyd
set_error_handler()?

The MySQL extension does not throw exceptions.

Posted: Tue Jan 30, 2007 3:12 am
by vin_hew
thanks

Posted: Tue Jan 30, 2007 4:46 am
by volka
But PDO throws excpetions. Simple example

Code: Select all

<?php
try {
	$dbh = new PDO('mysql:host=localhost;dbname=test', 'wronguser', 'wrongpass', array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
	// ...
}
catch(PDOException $e) {
	die('exception: ' . $e->getMessage() );
}
?>
see http://de3.php.net/pdo