I tried to display image from database,
path in database
require_once "Connection.php";
class DisplayDataImageProfile {
function showImageProfile(){
$connection = new Connection();
$conn = $connection->getConnection();
$id = $_GET['id'];
try{
$sqlDisplay = "SELECT photo FROM frofile WHERE id =$id";
$getImage = $conn->prepare($sqlDisplay);
$getImage->execute();
$getImage->fetchAll(PDO::FETCH_ASSOC);
foreach($getImage as $data){
header('Content-type: image/jpg');
// echo "<img src='$data'>";
echo $data;
}
}catch (PDOException $e){
echo "Error : " + $e->getMessage();
}
}}
after that i call in html page like this :
<img src="DisplayDataImageProfile .php?id=3" align="center" />
i got problem that image cannot retrieve from database using path. other case on webpage the image something like broken image displayed.
for currently i just display one image.
You need to use $data
as:
$data['photo']
instead of just
$data
Because $getImage
consist on associative array, so you can not use like your example, you need to use index for print data.
You should use the proper method.
Instead of fetchAll() which returns a nested array, you have to use fetchColumn() which returns single value. And you should be using prepared statements properly:
$sql = "SELECT photo FROM frofile WHERE id = ?";
$getImage = $conn->prepare($sqlDisplay);
$getImage->execute([$_GET['id']]);
header('Content-type: image/jpg');
echo $getImage->fetchColumn();
Edit: If you don't have an image itself in database but only path to the image, then you don't need this code at all. Get rid of Display.php and just select your path in the script that is echoing
<img src="Display.php?id=3" align="center" />
and echo selected path instead of Display.php?id=3
.