Ishi Silva
Ishi Silva

Reputation: 59

How to retrieve images from database in php

I have a database which has a table called 'propImages' and there are two columns.- 'pid' and 'location'.

And i have data in the database where multiple images can contained by single pid. image contains database data

now i want to retrieve images from database according to given pid. there can be more than one image. All i know it there should be an iteration to retrieve images.

I want to display images in HTML .

can you please show me the way to do it in php?

Thanks in advance guys

Upvotes: 0

Views: 1367

Answers (2)

Asif Uddin
Asif Uddin

Reputation: 409

<?php
     $servername = "localhost";
     $username = "root";
     $password = "";
     $dbname = "databasename";

     // Create connection
     $con = mysqli_connect($servername, $username, $password, $dbname);

     //create sql
     $sql = "SELECT * FROM `propImages` where pid='$YOUR_PID'";
     $result = mysqli_query($con, $sql);
     $row = mysqli_num_rows($result); 
     //retrive data print here
     if($row > 0){
         while($col = mysqli_fetch_assoc($result))
         {
             echo $col['location'];
         }
     } else {
        echo 'no result found.'; 
     }
?>

wish it helps

Upvotes: 1

shunz19
shunz19

Reputation: 534

This may help you

<?php
include 'inc/database.php';
$conn = new mysqli($servername, $username, $password, $database);
$propid = $_GET['propid'];
$sql = "SELECT * FROM propImages WHERE propid='" . $propid . "';";
$result = $conn->query($sql);

if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src=" . $row['image'] . ">";
}
}
else {
echo "No results";
}
?>

in the inc/database.php :

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "database";
?>

To see how it works try visiting : file.php?propid=22

Upvotes: 1

Related Questions