All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Fetch Data From Database In PHP And Display In HTML Table

Last Updated : Mar 11, 2024

How To Fetch Data From Database In PHP And Display In HTML Table

In this tutorial we will show you the solution of how to fetch data from database in PHP and display in HTML table in last tutorial we understand that how to fetch data in database and display database in HTML.

This concept is similar but with some minor difference that we have to show data in table that is created in html. Now, let us understand how to done this.

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

As, we many times say that there are many ways with the help of which we can do this.

We are going to show you this concept with the help of previous article but with some minor change.

Here, again our database name is myProject and table name is studentMarks. Let us understand the codes given below.

<?php
    $connect=mysqli_connect("localhost","root","","myProject") or die(" Connection Failed");
    $query="select * from studentMarks";
 $result=mysqli_query($connect,$query);
 while($row=mysqli_fetch_data($result))
{
>?
<table>
 <tr>
  <td>
   <?php
    echo $row[‘studentName’];
   ?>
  </td>
  <td>
   <?php
    echo $row[‘studentClass’];
   ?>
  </td>
  <td>
   <?php
    echo $row[‘studentRoll’];
   ?>
  </td>
  <td>
   <?php
    echo $row[‘studentMarks’];
   ?>
  </td>
 </tr>
</table>
<?php
}
?>
  1. Now, here let us discuss codes here. At first here we have to open and close php basic tags that are <?php ?>. Whereas <?php is opening tag for php codes and ?> is closing tag for php code. All the php content is written inside these codes.
  2. Now, one thing to note here that php codes can also be written inside html code. We hope that you know about the basic structure of html.
  3. If don’t know about basic structure of html then you can visit to our html tutorials in which we explain about basic structure of html.
  4. Here, we are going to show you fetched data in html table. Hence, we have to fetch data from database with the help of php and have to show data with the help of html codes.
  5. Now, here in these lines of codes we first established our connection with database using $connect.
  6. If you want to know about connection that how to establish connection between webpage and database then you must visit to our previous webpages in which we discuss about establishment of connection between database and webpage.
  7. Now, in query we have to use select * from studentMarks. Whereas the studentMarks is table name that we created inside database and * means all the columns inside table gets selected. You can select several or some columns rather than all also.
  8. Now, connection query is used here and saved or stored inside $result variable.
  9. At last step, we create a while loop and inside while mysqli_fetch_assoc() is used and stored inside row. This query is used to fetch data from database and the parameter given inside this is $result. Which helps this query that from which database and from which table inside database this query have to fetch data.
  10. After here we close php and have to write html codes to create a table, so that data fetched can be showed into table.
  11. In html codes we create table with the help of table, tr and td tags and then in td we have to show data fetched from database which is fetched with the help of php. So, we start php tags here and use echo to print data.
  12. We can also give header to table data in this article. We can show you a table with headers and also display data in that table in next article.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to fetch data from database and display it in html table. I hope this tutorial on how to fetch data from database in PHP and display in HTML table helps you.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪