Jump to content

Please expalin the use of this function


apysan

Recommended Posts

Hello friends, I have just begun to learn PHP. I prefer to learn in PDO way.
I write the following code to connect to phpmyadmin (wamp).
unable to understand the use of this function, even if it is useful why do we mention only two parameter with this, please help to
understand.
try{
$handler = new PDO('mysql:host=localhost;dbname=arjun_kaa_database','root','');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
//echo "An Error is Catched<br>"; //$e->getmessage();
//die("More Specific Message will be >--------------> Something Wrong with the Database");
echo $e->getMessage();
die();
}
Thanks and Regards
Link to comment
Share on other sites

What that line is doing is telling PDO to throw an exception if a MySQL error occurrs. This would mean you can catch MySQL errors using a try-catch block.

 

For example:

try {    // .. Some PDO database operations} catch(PDOException $e) {    echo 'MySQL error: ' . $e->getMessage();}

PDO's setAttribute() method just sets some of PDO's configuration.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...