All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Fetch The Data From Database In PHP

Last Updated : Mar 11, 2024

How To Fetch The Data From Database In PHP

In this article we will show you the solution of how to fetch the data from database in PHP, we fetched data from the MySQL database from the Localhost server and display it in an HTML table.

We used mysql_fetch_assoc() function in the example below to fetch data.


this function fetches a result from a database as an object.

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

At first, we created a database on the localhost server.

Now, we have to create a php file for connecting with the database.


mysql_connect('localhost', 'root', '') ;
mysql_select_db('demo') ;
  1. Using Mysql_connect() with the name of server, database name and password string
  2. Using My_select_db()

Main PHP Code

include_once('connection.php') ;
$query = "select * from student" ;
$result = mysql_query($query) ;
<!DOCTYPE html>
<html lang = " en " >
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> how to fetch the data from database in php </title>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> how to fetch the data from database in php </h2>
    <table align="center" border="1px solid black" style="width: 600px; line-height:30px;">
            <th colspan="4"><h2> student record </h2></th>
            <th> Name </th>
            <th> Email </th>
            <th> Contact </th>
            <th> Country </th>
     while($rows= mysql_fetch_assoc($result))
        <td><?php echo $rows['Name'] ; ?></td>
        <td><?php echo $rows['Email'] ; ?></td>
        <td><?php echo $rows['contact'] ; ?></td>
        <td><?php echo $rows['country'] ; ?></td>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Created a <table> with some inline CSS to style the table
  8. <tr> and <h2> to add row heading with inline CSS to the colspan ‘4’.
  9. Use <tr> and <th> to add headings of the table as Name, Email, Contact and Country
  10. Now creating a Php tag with the function mysql_fetch_assoc() with $result in the while loop.
  11. To display the table data we used <td> with inline php echo $row[ ]
  12. we write <?php tag to write PHP within it.
  13. Using include_once() with the name of the php file used to connect with the database
  14. Using $query to select the table from the database
  15. $result to pass the $query variable to the mysql_query().
  16. ?> to close the php code.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to fetch the data from database in php.

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

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪