Retrieve Data From MySQL Using PHP

Today, We want to share with you how to fetch data from database in php.In this post we will show you php code to retrieve data from mysql database and display, hear for how to get data from database in php using id we will give you demo and example for implement.In this post, we will learn about how to fetch data from database in php and display in table with an example.

how to retrieve data from database in php using mysqli

There are the Following The simple About how to fetch data from database in php and display in html table Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to fetch data from database in php and display in div, so the how to display data from database in php in table is used for this example is following below.

Read Also:  how to retrieve data from database in html form using PHP

For get or retrieve or fetch data from PHP MySQL the SELECT query statement is used. I can retrieve all the members data from specific table column or all column of a table in Database.

To get selected table column data from mysql database the SQL query is

SELECT specific_column_name1,specific_column_name2, ....., ..... FROM DB_table_name;

To retrieve or get all the table column data from a table the SQL query is

SELECT * FROM DB_table_name;

In the below simple php example i retrieve the data from MySQL database.

In this example i used main 2 file for retrieve fetch data all the members

  • dbConfig.php- To connecting database.
  • retrieve.php- For retrive data from MySQL database
Read Also:  how to fetch data from database in php using search button?

Step 1: connecting database

dbConfig.php

<?php
$url='127.0.0.1:3306';
$username='root';
$password='';
$conn=mysqli_connect($url,$username,$password,"pakainfo");
if(!$conn){
 die('Could not Connect My Sql:' .mysql_error());
}
?>

Step 2: PHP retrive data from database

retrieve.php

<?php
include_once 'dbConfig.php';
$result = mysqli_query($conn,"SELECT * FROM mymembers");
?>
<!DOCTYPE html>
<html>
 <head>
 <title> Retrive data - www.pakainfo.com</title>
 </head>
<body>
<?php
if (mysqli_num_rows($result) > 0) {
?>
  <table>
  
  <tr>
    <td>First Name</td>
    <td>Last Name</td>
    <td>City</td>
    <td>Email id</td>
  </tr>
<?php
$i=0;
while($data = mysqli_fetch_array($result)) {
?>
<tr>
    <td><?php echo $data["profile_nm"]; ?></td>
    <td><?php echo $data["real_nm"]; ?></td>
    <td><?php echo $data["full_address"]; ?></td>
    <td><?php echo $data["email"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
 <?php
}
else{
    echo "No result found";
}
?>
 </body>
</html>

Step 2: Create a Custom CSS File

style.css

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;

}

tr:nth-child(even) {
    background-color: white;
}

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to fetch data from database in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.