How To Fetch Data From Database In PHP
Last Updated : Jul 1, 2023
IN - PHP MySQL | Written & Updated By - Riya
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.
<?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)) { //here is code for display data, which we will discuss in next session, here we are going to fetch data only. } ?>
- 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.
- 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.
- 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.
- Now, here in these lines of codes we first established our connection with database using $connect.
- 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.
- 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.
- Now, connection query is used here and saved or stored inside $result variable.
- 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.
- 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.