提问人:kalel681 提问时间:10/7/2020 更新时间:10/8/2020 访问量:91
使用自动完成选择从数据库中检索 BLOB 图像
Retrieving BLOB image from database using autocomplete selection
问:
我正在尝试显示存储在数据库中的 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'] ).'"/>';
?>
有什么想法吗?
答:
0赞
kalel681
10/8/2020
#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'] ).'"/>';
评论
$result['emp_img']