Database connection using PDO

0
1157

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. 0.1

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

  9. Now to create the database go to the following link as shown in the picture below:
  10. 0.3

  11. After clicking on the SQL tab following window will appear.
  12. 0.4

  13. Now create database with following command:
  14. 0.5

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

  17. Now write the following command to create the database and insert values into the table :
  18. Thus you will have the following view of your window :
  19. 0.7

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

  22. Now go to Xampp -> htdocs folder and create a new folder say Pdo .
  23. 1

  24. Now in this Pdo folder create a new file say index.php
  25. 1212121

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

  29. 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 :
  30. After editing the index.php file you will have the following view of your text editor window :
  31. 4

  32. Now enter the following url to get the output http://localhost/Pdo/
  33. 5.0

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

  37. 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 :
  38. 6

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

  41. So replace the code of index.php file with the following one :
  42. You will have the following view of your text editor window :
  43. 8

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

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

  48. Now replace the code of index.php with the following one :
  49. Screen shot of the code is shown below :
  50. 11

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

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here