How to connect to the ORACLE using PDO?

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

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

<?php
	$user='root'; // Enter your DB User Name.
	$pass=''; // Enter your DB Password.
	$dataBaseName='webexpertdeveloper'; // Enter your Database Name.
	$dbh = new PDO('OCI:dbname='.$dataBaseName.'charset=UTF-8', $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.
		$dataBaseName='webexpertdeveloper'; // Enter your Database Name.
		$dbh = new PDO('OCI:dbname='.$dataBaseName.'charset=UTF-8', $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.

Closing a connection

<?php
	try
	{
		$user='root'; // Enter your DB User Name.
		$pass=''; // Enter your DB Password.
		$dataBaseName='webexpertdeveloper'; // Enter your Database Name.
		$dbh = new PDO('OCI:dbname='.$dataBaseName.'charset=UTF-8', $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 ORACLE 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