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

Last Updated : Mar 11, 2024

How To Fetch Data From Database In PHP And Display

In this tutorial we will show you the solution of how to fetch data from database in PHP and display and we are going to learn about how we are able to fetch data from database in PHP and display that also.

As we know that data is stored in database and today we are going to fetch that data from database and display it to html screen.

In last tutorials, we understand that how we are able to fetch data, insert data and make a connection between webpage and database.

Now, here below we understand that how we are going to understand that how to fetch and display data. Let’s understand.

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

As, there are many ways with the help of which we are able to fetch and display data.

For our example, let’s assume that our database name is myProject and our table name inside database is studentMarks. Now, with the help of these assumptions let’s create a code to fetch and display data.

<?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))
{
 echo $row[‘student_name’];
 echo $row[‘student_class’];
 echo $row[‘student_rollno’];
 echo $row[‘student_marks’];
}
?>
  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. 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.
  3. In case you want to display data in table or any html tag after fetching data from database on webpage then it is mandatory to use html in codes. In this case the fetching data query of php can be written anywhere in html codes but display data query must be written inside body of html.
  4. Now, here in these lines of codes we first established our connection with database using $connect.
  5. 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.
  6. 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.
  7. Now, connection query is used here and saved or stored inside $result variable.
  8. 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.
  9. After we use echo now to print data and after echo we use $row and inside $row we have to give column name inside square brackets. Here we have to get the column name from database means that the columns name must be same to database inside which we store data.

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

I hope this tutorial on how to fetch data from database in PHP and display helps you.