How to capture exceptional error when connect to Mysql DB

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
vin_hew
Forum Newbie
Posts: 2
Joined: Sun Jan 28, 2007 10:09 pm

How to capture exceptional error when connect to Mysql DB

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

set_error_handler()?

The MySQL extension does not throw exceptions.
vin_hew
Forum Newbie
Posts: 2
Joined: Sun Jan 28, 2007 10:09 pm

Post by vin_hew »

thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply