How To Display Data From Database Table In PHP/MySQL Using PDO Query

In this tutorial, we are going to learn on How To Display Data From Database Table In PHP/MySQL Using PDO Query. You can use this source code to merge the last tutorial that I made and it's called Registration Form In PHP/MySQL Using PDO Query. This source code will help us on how to show data from the Database using PDO Query. Let's start with:

Creating our Table

We are going to make our database. To create a database:
  1. Open the PHPMyAdmin.
  2. Create a database and name it as "registration_pdo".
  3. After creating a database name, then we are going to create our table. And name it as "user_registration".
Kindly copy the code below.
  1. --
  2. -- Table structure for table `user_registration`
  3. --
  4.  
  5. CREATE TABLE `user_registration` (
  6. `user_id` INT(11) NOT NULL,
  7. `first_name` VARCHAR(100) NOT NULL,
  8. `last_name` VARCHAR(100) NOT NULL,
  9. `user_name` VARCHAR(100) NOT NULL,
  10. `password` VARCHAR(100) NOT NULL
  11. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Data in the Database. Result
  1. --
  2. -- Dumping data for table `user_registration`
  3. --
  4.  
  5. INSERT INTO `user_registration` (`user_id`, `first_name`, `last_name`, `user_name`, `password`) VALUES
  6. (1, 'Jane', 'Doe', 'jane24', 'doe23jd'),
  7. (2, 'John', 'Doe', 'john43', 'john_doe325kh'),
  8. (3, 'Sarah', 'Geronimow', 'sarahGero', 'geronimow243'),
  9. (4, 'Super', 'Man', 'returns', 'superman_returns');
We are going to make our database connection.

Database Connection

Copy and paste this then save it as "connection.php".
  1. <?php
  2. $db_server = "localhost";
  3. $db_username = "root";
  4. $db_password = "";
  5. $db_database = "registration_pdo";
  6.  
  7. $conn = new PDO("mysql:host=$db_server;dbname=$db_database", $db_username, $db_password);
  8. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. ?>
Then, we are creating a new page where our data is to display.

Display Page

This source code for displaying data in the database table and save it as "index.php".
  1. <table border="1" cellspacing="5" cellpadding="5" width="100%">
  2. <thead>
  3. <tr>
  4. <th>No.</th>
  5. <th>First Name</th>
  6. <th>Last Name</th>
  7. <th>User Name</th>
  8. <th>Password</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <?php
  13. require_once('connection.php');
  14. $result = $conn->prepare("SELECT * FROM user_registration ORDER BY user_id ASC");
  15. $result->execute();
  16. for($i=0; $row = $result->fetch(); $i++){
  17. ?>
  18. <tr>
  19. <td><label><?php echo $row['user_id']; ?></label></td>
  20. <td><label><?php echo $row['first_name']; ?></label></td>
  21. <td><label><?php echo $row['last_name']; ?></label></td>
  22. <td><label><?php echo $row['user_name']; ?></label></td>
  23. <td><label><?php echo $row['password']; ?></label></td>
  24. </tr>
  25. <?php } ?>
  26. </tbody>
  27. </table>
And, this is the style.
  1. <style type="text/css">
  2. body {
  3. width:800px;
  4. border:red 1px solid;
  5. border-style:dashed;
  6. margin:auto;
  7. padding:10px;
  8. }
  9. td {
  10. text-align:center;
  11. padding:10px;
  12. }
  13. table {
  14. margin:auto;
  15. border:blue 1px solid;
  16. }
  17. label {
  18. font-size:18px;
  19. color:blue;
  20. font-weight: bold;
  21. font-family: cursive;
  22. }
  23. h2 {
  24. color:red;
  25. text-align:center;
  26. }
  27. th {
  28. color:red;
  29. font-size:20px;
  30. font-family: cursive;
  31. }
  32. </style>

Output:

You can see in the image lists of data in the Database. Result This is all the steps on How To Display Data From Database Table In PHP/MySQL Using PDO Query. So, this is it, or you can download the full source code below by clicking the "Download Code" button below. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Comments

Submitted bympholeng m (not verified)on Fri, 07/12/2019 - 15:50

this is helpful,thank you
Submitted byZedrick Khan (not verified)on Thu, 12/12/2019 - 19:42

I just learned how create a table for `user_registration` in PHP #ceos #markzuckerberg #satyanadella #libra #workingfromhome CREATE TABLE `user_registration` ( `user_id` INT(11) NOT NULL, `first_name` VARCHAR(100) NOT NULL, `last_name` VARCHAR(100) NOT NULL, `user_name` VARCHAR(100) NOT NULL, `password` VARCHAR(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=Indoamerican
Submitted byGabriele (not verified)on Sun, 01/12/2020 - 05:28

Thanks for this tutorial but... I'd like to see after login just the user's details and also I'd like just to modify them, I don't understand how to use sessions... :\

Add new comment