how to get data from database php code example
Example 1: query sql in php
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Example 2: how to fetch data from database in php
<?php
$serve="localhost";
$user="root";
$password="";
$db="neha";
$con=mysqli_connect($serve,$user,$password,$db);
if ($con) {
echo "connection success";
}else
{echo "connection unsuccess"; }
$query="SELECT * FROM product";
$result=$con->query($query);
if($result->num_rows>0){
while($row=$result->fetch_assoc())
{
echo $name=$row['name'];
echo $qty=$row['qty'];
echo $price=$row['price'];
echo $image=$row['image'];
}
}
?>
Example 3: how to fetch data from database in php
<?php
include 'config.php';
$stmt = $conn->prepare('SELECT * FROM `product`');
$stmt->execute();
$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>