All TalkersCode Topics

Follow TalkersCode On Social Media

How To Display Data From Database In PHP In Table

Last Updated : Jan 1, 2023

How To Display Data From Database In PHP In Table

In this article we will show you the solution of how to display data from database in PHP in table, here we needs to execute database connection to interact with database table data then needs to define sql query for select table data.

After that we have to connect this query with database connection so it will executes query at database then it returns all data now we have to display those data in table so we have to use loop for iterate each of them and print on webpage table.

Step By Step Guide On How To Display Data From Database In PHP In Table :-

Here we need to use mysqli_connect() method for execute connection with database ‘dbase’ and it refers ‘$conn’ then for select all data from table ‘info’ we defined select query and it stored on variable ‘$qry’ then connection $conn points select query ‘$qry’.

It will returns data of table ‘info’ records that is stored on variable $result.

In html block we defined table structure with header column names ‘ID, Name, Password’ then we using while loop for iterate all table data using ‘fetch_assoc()’.

Here variable ‘$row’ refers each table row data’s then we printing each column data using ‘echo’ within <td> column tag.

$conn = mysqli_connect('localhost','root','','dbase');
    echo "connected successfully";
        echo "Error";
    $qry="SELECT * FROM info";
<table border="1">
        <td><?php echo $rows['ID'];?></td>
        <td><?php echo $rows['Name'];?></td>
        <td><?php echo $rows['Password'];?></td>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined values ‘localhost,root,’’,’dbase’ for make connection with server database. Using mysqli_connect() method we executed database connection with those values and connection referred by variable ‘$conn’.
  4. Using if condition we are checking database connection whether exists or not then printed respective result on webpage.
  5. Defined select query with table name ‘info’ for select all row values from table and it is stored on variable ‘$qry’.
  6. We know query() method will executes query what we defined at database so we used this method for executes this select query at database by pointing with $conn connection variable then it returns either all collected table records or null that is stored on variable ‘$result’.
  7. For printing database table records we need html table structure so we defined table in html block with table headers ‘ID,Name,Password’.
  8. When collecting table records it contains more row of values so here we needs to use loop that’s way we are used while loop with fetch_assoc() method.
  9. Then we printed all row values column by column when specifying ‘ID,Name,Password’ column names with ‘$rows’ variable using echo statement between <td> tags.

Conclusion :-

In conclusion now we are able to know how to display data from database in table using php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it will displays all retrieved table ‘info’ records from database ‘dbase’ in html table on webpage.

I hope this tutorial on how to display data from database in PHP in table helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials