如何使用PHP从数据库中检索图像

时间:2016-03-13 17:50:05

标签: php html mysql image

我正在使用DB中的以下代码检索器图像以及其他属性,即全名,手机号等。但它显示的是一个空的图像框。

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>

1 个答案:

答案 0 :(得分:1)

在获取$row = $data or die("line 44 not working");

之前,您已断开数据库连接

设置变量后需要断开连接。

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>