【问题标题】:PHP using XAMPP Displaying BLOB img as an image not Binary codePHP使用XAMPP将BLOB img显示为图像而不是二进制代码
【发布时间】:2018-04-10 13:45:27
【问题描述】:

我正在尝试使用 PHP 回显数据库以显示我的整个表格,包括图像,但是当涉及到图像时,我得到的只是二进制代码。请帮忙:

PHP

<!Doctype html>
<html>
    <head>
        <title>connecting to a Database</title>
    </head>
    <body>
        <?php 
        $con = mysql_connect('localhost','root','');
        $db = mysql_select_db('dinnerpartydata');

      if($con){
          echo 'Successfully connected to the database.';
      }else {
          die('Error.');
      }
      if($db){
          echo 'Successfully found the database';
      }else {
          die('Error. database not found.');
      }
      ?>
<br />
<br />
      <?php 
        $query= mysql_query("SELECT * from recipesnimages");
        while($row = mysql_fetch_array($query)){
            $id = $row['id'];
            $recipeType = $row['recipeType'];
            $recipeName = $row['recipeName'];
            $Instructions = $row['Instructions'];
            $recipeImg = $row['recipeImg'];

            echo $id . '| ' . $recipeType . '| ' . $recipeName . '| ' . $Instructions .'| ' . $recipeImg .'<br />';

        }
      ?>

    </body>
</html>

【问题讨论】:

  • 您可能需要将其转换为图像并放入您正在打印的网页中: echo '”。当然,取决于您使用的确切格式。
  • 嘿 kiteflight,我使用的是 150X150 的 jpg。我无法理解这种转换是如何工作的。你能用我的代码回复一个例子吗?
  • 尝试像这样更改您的代码(仅当图像使用 base64 编码保存在数据库中时才有效,否则您必须适应您使用的格式): echo $id 。 '| ' . $recipeType 。 '| ' . $食谱名称。 '| ' . $指令。'| ' . .'
    ';

标签: php mysql database xampp backend


【解决方案1】:

如果有人感兴趣,这里是这个问题的答案:

连接到数据库

  if($con){
      echo 'Successfully connected to the database.';
  }else {
      die('Error.');
  }
  if($db){
      echo 'Successfully found the database';
  }else {
      die('Error. database not found.');
  }
  ?> <br /> <br />
  <?php 
    $query= mysql_query("SELECT * from recipesnimages");
    while($row = mysql_fetch_array($query)){
        $id = $row['id'];
        $recipeType = $row['recipeType'];
        $recipeName = $row['recipeName'];
        $Instructions = $row['Instructions'];
        $recipeImg = $row['recipeImg'];

        echo $id . '| ' . $recipeType . '| ' . $recipeName . '| ' . $Instructions .'| '. '<img

src="data:image/jpeg;base64,'.base64_encode($recipeImg).'" 高度=“150”宽度=“200”/>'; 回声'
';

    }
  ?>

</body> </html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2021-10-29
    • 1970-01-01
    • 2015-11-12
    • 2016-03-04
    相关资源
    最近更新 更多