2016-05-17 216 views
1

我试图插入属于员工资料的图像。从数据库插入图像(BLOB)Echo

我的代码:

<?php 
    $sql = "SELECT * FROM employeeTbl where department='aarhus';"; 
    $rows=$conn->query($sql); 
    foreach ($rows as $row){ 
     echo '<div class="col-xs-6 col-sm-3" style="min-height:225px;overflow:hidden; margin-top:30px">; 
?> 

<img src="getimage.php?aid=<?php echo $row["aid"]; ?>" /> 

<?php 
     echo '<h2 style="margin-top:-10px">'.$row["name"].'</h2>'; 
     echo '<i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;<a href="mailto:'.$row["mail"].'">' .$row["mail"].'</a>';  
     echo "</div>"; 
    } 
?> 

我getimage.php代码:

<?php 
    $conn = mysql_connect("mysql34.unoeuro.com", "user", "pass"); 
    mysql_select_db("dbname") or die(mysql_error()); 

    if(isset($_GET['aid'])) { 
      $sql = "SELECT image FROM employeeTbl WHERE aid=" . $_GET['aid']; 
      $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error()); 
      $row = mysql_fetch_array($result); 
      header('Content-Type: image/jpeg'); 
      echo $image; 
    } 
    mysql_close($conn); 
?> 

我的数据库中的援助是自动递增。

希望你能帮助我 - 或找到一个更好的解决方案。

+0

你不应该使用mysql_功能。自PHP 5.5起,它们已被弃用,并在PHP 7.0中被删除。你应该使用mysqli_函数(例如mysqli_query),它需要两个参数。 http://php.net/manual/en/mysqli.query.php 或者使用PDO :: functions –

回答

0

第一步mysql不推荐使用,我建议使用mysqli函数。

关于创建图像我建议下面的代码:

$data = base64_decode($image); 
$im = imagecreatefromstring($data); 
if ($im !== false) { 
    header('Content-Type: image/png'); 
    imagepng($im); 
    imagedestroy($im); 
}