【问题标题】:Retrieving BLOB image from database using autocomplete selection使用自动完成选择从数据库中检索 BLOB 图像
【发布时间】:2021-01-21 19:50:49
【问题描述】:

我正在尝试显示存储在数据库中的 blob 图像,我没有收到任何错误但图像没有显示,我只是获得默认的“无图像”图标。这是我的代码:

<script>
  function showEmpimg(str) {
    var xhttp;
    if (str == "") {
      document.getElementById("user-id").innerHTML = "";
      return;
    }
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
      document.getElementById("face").innerHTML = this.responseText;
      }
    };
    xhttp.open("POST", "getimage.php?q="+str, true);
    xhttp.send();
  }
</script>
</head>
<body>
  <div id="face" class="face"> 
  </div>
  <input type="text" class="form-control" id="user-id" placeholder="ID" name="emp_id" onchange="showEmpimg(this.value)" required maxlength="6" />

还有php文件:

<?php

$db = mysqli_connect("localhost","root","test1","dar");
$sql = "SELECT emp_img FROM employees WHERE emp_id LIKE 'q'";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['emp_img'] ).'"/>';

?>

有什么想法吗?

【问题讨论】:

  • $result['emp_img']的内容是什么?
  • 我应该是一个 BLOB .jpg 文件。从数据库。执行后检查代码时,img src 显示“data:image/jpeg;base64”,但其余部分不显示。

标签: javascript php database image xhtml


【解决方案1】:

已解决:

$db = mysqli_connect("localhost","root","test1","dar");
$sql = "SELECT * FROM employees WHERE emp_id=" . intval($_GET['q']);
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);

 //var_dump($result);
echo '<img class="face" src="data:image/jpeg;base64,'.base64_encode( $result['emp_img'] ).'"/>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-23
    • 2014-02-19
    • 2021-09-26
    • 1970-01-01
    • 2013-04-26
    • 2014-06-29
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多