Example 1: query sql in php

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"
; } } else { echo "0 results"; } $conn->close();

Example 2: php code for fetching data from database

DOCTYPE html>
<html>
<head>
<title>Read Data From Database Using PHP - Demo Previewtitle>
<meta content="noindex, nofollow" name="robots">
<link href="style.css" rel="stylesheet" type="text/css">
head>
<body>
<div class="maindiv">
<div class="divA">
<div class="title">
<h2>Read Data Using PHPh2>
div>
<div class="divB">
<div class="divD">
<p>Click On Menup>

$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("company", $connection); // Selecting Database
//MySQL Query to read data
$query = mysql_query("select * from employee", $connection);
while ($row = mysql_fetch_array($query)) {
echo ".php?id={$row['employee_id']}">{$row['employee_name']}";
echo "
"
; } ?>
div> if (isset($_GET['id'])) { $id = $_GET['id']; $query1 = mysql_query("select * from employee where employee_id=$id", $connection); while ($row1 = mysql_fetch_array($query1)) { ?> <div class="form"> <h2>---Details---h2> <span>Name:span> echo $row1['employee_name']; ?> <span>E-mail:span> echo $row1['employee_email']; ?> <span>Contact No:span> echo $row1['employee_contact']; ?> <span>Address:span> echo $row1['employee_address']; ?> div> } } ?> <div class="clear">div> div> <div class="clear">div> div> div> mysql_close($connection); // Closing Connection with Server ?> body> html> Copy

Example 3: php code for fetching data from database

CREATE DATABASE company;
CREATE TABLE employee(
employee_id int(10) NOT NULL AUTO_INCREMENT,
employee_name varchar(255) NOT NULL,
employee_email varchar(255) NOT NULL,
employee_contact varchar(255) NOT NULL,
employee_address varchar(255) NOT NULL,
PRIMARY KEY (employee_id)
)
Copy

Example 4: how to fetch data from database in php


   //////neha jaiswal////
       include 'config.php';////incluude data base connection file
        $stmt = $conn->prepare('SELECT * FROM `product`');///select all data from database
        $stmt->execute();////query execute ($stmt)
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()):
      ?>
         <div class="card p-2 border-secondary mb-2">
            <img src=" $row['product_image'] ?>" class="card-img-top" height="250">
            <div class="card-body p-1">
              <h4 class="card-title text-center text-info"> $row['product_name'] ?>h4>
              <h5 class="card-text text-center text-danger"><i class="fas fa-rupee-sign">i> number_format($row['product_price'],2) ?>/-h5>