Retrieve Data From MySQL Using PHP Tutorial

Data are often fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. you’ve got several options to fetch data from MySQ

For retrieve data from MySQL the SELECT statement is employed we will retrieve data from specific column or all column of a table.

For retrieve selected column data from database the SQL query is

SELECT column1,column2 FROM table_name;

For retrieve all the column data from a table the SQL query is

SELECT * FROM table_name;
In this tutorial we retrieve the data from MySQL database
In this tutorial having 2 files for retrive data
database.php – To connecting database
retrieve.php – For retrive data from database
<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "user_details";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
   die('Could not Connect My Sql:' .mysql_error());
}
?>
<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM details");
?>
<!DOCTYPE html>
<html>
 <head>
 <title> Retrive data</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($row = mysqli_fetch_array($result)) {
?>
<tr>
    <td><?php echo $row["firstname"]; ?></td>
    <td><?php echo $row["lastname"]; ?></td>
    <td><?php echo $row["city"]; ?></td>
    <td><?php echo $row["email"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
 <?php
}
else{
    echo "No result found";
}
?>
 </body>
</html>

2 thoughts on “Retrieve Data From MySQL Using PHP Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *