Zaid
Zaid

Reputation: 79

How to retrieve image from DB using PHP

I'm using the following code retriever image from DB along with other attributes i.e Full Name, Mobile No. etc.But it is showing an empty image box.

require 'database.php';
$MobileNo = null;
if ( !empty($_GET['MobileNo'])) {
$MobileNo = $_REQUEST['MobileNo'];
}

if ( null==$MobileNo ) {
header("Location: index.php");
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM user where MobileNo = ?";
$q = $pdo->prepare($sql);
$q->execute(array($MobileNo));
$data = $q->fetch(PDO::FETCH_ASSOC);
Database::disconnect();
}
?>
<div class="control-group">
<label class="control-label">Picture</label>
<div class="">
<label class="">
<?php

 $row = $data or die("line 44 not working");
 $s=$row['Picture'];
 echo $row['Picture'];

 echo '<img src="'.$s.'" alt="HTML5 Icon"style="width:128px;height:128px">';
 ?>

 </label>
 </div>
 </div>

Upvotes: 0

Views: 60

Answers (1)

user5831063
user5831063

Reputation:

You have disconnected the database before fetching $row = $data or die("line 44 not working");

You need to disconnect it after the variable is set.

if ( null==$MobileNo ) {
header("Location: index.php");
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM user where MobileNo = ?";
$q = $pdo->prepare($sql);
$q->execute(array($MobileNo));
$data = $q->fetch(PDO::FETCH_ASSOC);


$row = $data or die("line 44 not working");
 $s=$row['Picture']; //This is where you make the change. 
 echo $row['Picture'];
Database::disconnect(); //Now disconnect
}
?>
<div class="control-group">
<label class="control-label">Picture</label>
<div class="">
<label class="">
<?php



 echo '<img src="'.$s.'" alt="HTML5 Icon"style="width:128px;height:128px">';
 ?>

 </label>
 </div>
 </div>

Upvotes: 1

Related Questions