All TalkersCode Topics

Follow TalkersCode On Social Media

How To Fetch Data From Database In PHP

Last Updated : Jan 1, 2023

How To Fetch Data From Database In PHP

In this tutorial we will show you the solution of how to fetch data from database in PHP, 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 can connect webpage to MySQL and how to insert data in database from html webpage.

If someone have any issue regarding that then he/she must visit to our articles. Let’s understand today’s article.

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

As, there are many ways with the help of which we are able to fetch 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.

    $connect=mysqli_connect("localhost","root","","myProject") or die(" Connection Failed");
    $query="select * from studentMarks";
 //here is code for display data, which we will discuss in next session, here we are going to fetch data only.
  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 after fetching data from database on webpage then it is mandatory to use html in webpage and php inside webpage 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.
  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.
  9. 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. Now, this is full query to fetch data, and we will discuss display data with the help of fetch data in next session.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to fetch data from database, now our next job is to display data from database. I hope this tutorial on how to fetch data from database in PHP helps you.

Latest Tutorials