Database connection using PDO


In this section we will be using Database connection using PDO (PHP Data Objects).

  • To Connect the database using PDO follow the steps given below :

  1. Install Xampp server and if it is already installed then go to the xampp control pannel and start the Apache and MySQL modules .
  2. First we have to create our database through which we can access the data.
  3. Now open up your web browser and enter the given url http://localhost/phpmyadmin/
  4. The following window will be displayed :

  5. Now fill the appropriate details inside it and click on go button.
  6. After clicking the Go button following window will get displayed :

  7. Now to create the database go to the following link as shown in the picture below:

  8. After clicking on the SQL tab following window will appear.

  9. Now create database with following command:

  10. So now after clicking on the GO button your database will be created at the left hand side as shown in the image :

  11. Now write the following command to create the database and insert values into the table :
  12. Thus you will have the following view of your window :

  13. So now click on the Go button and you will have your database created with the inserted data :

  14. Now go to Xampp -> htdocs folder and create a new folder say Pdo .

  15. Now in this Pdo folder create a new file say index.php

  16. Now open the index.php file in any text editor and write the following code for the connection with the database.
  17. Here is the screenshot of the editor :

  18. Now we have to write an SQL query and fetch it and display the output in the table format , so replace the index.php file with the following code :
  19. After editing the index.php file you will have the following view of your text editor window :

  20. Now enter the following url to get the output http://localhost/Pdo/

  21. So now lets insert some data into it , so replace the code in index.php file with following one :
  22. Here is the screen shot of the text editor :

  23. So now run the index.php file by entering the url as http://localhost/Pdo .It will automatically insert data into the database and you will have the following output :

  24. Now suppose you want to perform some update operations on your database for example : change the department and email of the first_name Lisa .

  25. So replace the code of index.php file with the following one :
  26. You will have the following view of your text editor window :

  27. Now to run it just enter the url as http://localhost/Pdo , you will have the following output :

  28. Now let’s delete some row from the database for example : Suppose if we want to delete the id = 12.

  29. Now replace the code of index.php with the following one :
  30. Screen shot of the code is shown below :

  31. Now to get the output enter the following url http://localhost/Pdo/ in the browser :

  32. Thus in this section we have done all the CRUD operations i.e. create, read , update and delete statement with the help of PDO.


Please enter your comment!
Please enter your name here