【问题标题】:Insert image from database (BLOB) Echo从数据库(BLOB)插入图像回声
【发布时间】:2016-05-17 17:14:21
【问题描述】:

我正在尝试插入属于员工个人资料的图像。

我的代码:

<?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);
?> 

我的数据库是自动递增的。

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

【问题讨论】:

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

标签: php mysql database image echo


【解决方案1】:

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

关于创建图像我建议使用以下代码:

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

【讨论】:

    猜你喜欢
    • 2017-03-23
    • 2016-10-09
    • 2014-07-14
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多