pull data from sql 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) {
    // output data of each row
    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
  ////////////neha jaiswal///
$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'];
 	}
 }

?>

Tags:

Php Example