PDO-Database-connections-and-abstraction

How to connect to the MYSQL using PDO?

Today I explain you how to connect with the MYSQL database using PDO.

In PDO(PHP Data Object) it’s very easy to connect with MYSQL.  As per below example you can easily  understand.

<?php
	$user='root'; // Enter your DB User Name.
	$pass=''; // Enter your DB Password.
	$hostName='localhost'; // Enter your host name.
	$dataBaseName='webexpertdeveloper'; // Enter your Database Name.
	$dbh = new PDO('mysql:host='.$hostName.';dbname='.$dataBaseName, $user, $pass);
	echo "Connection Successful";
?>

If there are any connection errors, a PDOException object will be thrown. You may catch the exception if you want to handle the error condition. For that you have to use below example you have to handle exception using try-catch .

<?php
	try
	{
		$user='root'; // Enter your DB User Name.
		$pass=''; // Enter your DB Password.
		$hostName='localhost'; // Enter your host name.
		$dataBaseName='webexpertdeveloper'; // Enter your Database Name.
		$dbh = new PDO('mysql:host='.$hostName.';dbname='.$dataBaseName, $user, $pass);
		echo "Connection Successful";
	}
	catch (PDOException $e)
	{
		print "Error!: " . $e->getMessage() . "
";
		die();
	}
?>

If any error occur then PDOException handle this and give you proper error message.

If you want to Persistent connections using PDO then follow below example.

Many web applications will benefit from making persistent connections to database servers. Persistent connections are not closed at the end of the script, but are cached and re-used when another script requests a connection using the same credentials. The persistent connection cache allows you to avoid the overhead of establishing a new connection every time a script needs to talk to a database, resulting in a faster web application.

<?php
	try
	{
		$user='root'; // Enter your DB User Name.
		$pass=''; // Enter your DB Password.
		$hostName='localhost'; // Enter your host name.
		$dataBaseName='webexpertdeveloper'; // Enter your Database Name.
		$dbh = new PDO('mysql:host='.$hostName.';dbname='.$dataBaseName, $user, $pass,array(PDO::ATTR_PERSISTENT => true));
		echo "Connection Successful";
	}
	catch (PDOException $e)
	{
		print "Error!: " . $e->getMessage() . "
";
		die();
	}
?>

Closing a connection

<?php
	try
	{
		$user='root'; // Enter your DB User Name.
		$pass=''; // Enter your DB Password.
		$hostName='localhost'; // Enter your host name.
		$dataBaseName='webexpertdeveloper'; // Enter your Database Name.
		$dbh = new PDO('mysql:host='.$hostName.';dbname='.$dataBaseName, $user, $pass,);
		echo "Connection Successful";
                $dbh = null; // It Will Close the connection
	}
	catch (PDOException $e)
	{
		print "Error!: " . $e->getMessage() . "
";
		die();
	}
?>
Advertisements

2 thoughts on “How to connect to the MYSQL using PDO?

Leave your comments

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s