PDO ManualPHP Tutorials

How to create database connection using PDO – PHP Data Objects (PDO) Manual – Part 1

In this academic article we discuss about How to create database connection using PDO.  You will also learn inequality between database connection and persistent database connection in PDO.  PDO stands PHP Data objects which is upgrade version of mysqli. PDO is a PHP class that is preinstall in all latest PHP versions. We have already discussed about inequality between Mysql, Mysqli and PDO.

How to create database connection in PHP Data Objects (PDO)  ?

For creating database connection using PDO, you have to create a database in SQL. After that copy the below code and paste  it in your file.

<?php
try {
   //Trying to connect database connection
   $my_connect = new PDO("mysql:host=<!--your mysql host-->;dbname=<!--your database name-->", <!--Database Username-->, <!--Database Password-->);
} catch(PDOException $e){
    //Error in connection will be here
	echo $e;
}
?>

In PDO, we cannot use if statement to show errors like we do in mysql and mysqli. PDO statement always return true values, so we use try statement in which we catch PDO Exception errors. Replace comments with your database credentials as follows

  • <!–your sql host–> with your mysql host like localhost or your server host.
  • <!–your database name–> with your database name that you created using PHP My admin.
  • <!–Database Username–> with your database username that you assign to your database.
  • <!–Database Password–> with your database user password.

Once you have filled all details, then run your file in browser. If page shows zero errors while serving then you are successfully connected to database using PDO. In above code we use new PDO for calling PDO class. Sometimes your server may not have pre installed PDO class in that case contact your server support.

But above connection is closed when the script ends. So whenever you visit different page then a new connection is established. It means whenever you run your script a new database connection is connected. These multiple connections can affect SQL server  if your users on website increased. To prevent these multiple connections PDO has a option for persistent connection.

What is persistent database connection ?

In persistent database connectivity, you can check your previous connection connectivity. In simple words, persistent connection checks previous connection before creating new connection. If your previous connection with database exists then PDO will use that connection instead of creating new one. These type of connections is called persistent connections. Persistent connections are not closed by the end of script, they are just cached and saved for reuse. Preventing multiple database connections in your application will increase application speed.

Syntax for creating persistent connection in PDO

<?php
try {
   //Trying to connect database connection
   $my_connect = new PDO("mysql:host=<!--your mysql host-->;dbname=<!--your database name-->", <!--Database Username-->, <!--Database Password-->, array(
    PDO::ATTR_PERSISTENT => true));
} catch(PDOException $e){
    //Error in connection will be here
	echo $e;
}
?>

 

Tags
Show More

Anil Mehra

I am Anil Mehra, a passionate, workaholic and a Full Stack Tech Savvy Programmer with true north towards growth. I have worked on 256 live projects in MNC. I am expertise in the field of Programming, Server Management, SEO, Blogging and SMO...

Related Articles

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close