Php 如何显示数据库中的图像?(菲律宾)
我在数据库中设置了一个图像表,将图像存储为blob类型。我的问题是,我不知道如何在我的web搜索页面的db中显示图像。每当我输入searh查询时,它都会显示关键字&图像名称,但不会显示图像本身。相反,它显示长的sql代码 以下是我的Imagesearch.php的php代码Php 如何显示数据库中的图像?(菲律宾),php,search-engine,Php,Search Engine,我在数据库中设置了一个图像表,将图像存储为blob类型。我的问题是,我不知道如何在我的web搜索页面的db中显示图像。每当我输入searh查询时,它都会显示关键字&图像名称,但不会显示图像本身。相反,它显示长的sql代码 以下是我的Imagesearch.php的php代码 <style type="text/css"> body { background-color: #FFF; } </style> <?php //get data $button =
<style type="text/css">
body {
background-color: #FFF;
}
</style>
<?php
//get data
$button = $_GET['submit'];
$search = $_GET['search'];
$x = "";
$construct = "";
if (!$button){
echo "You didint submit a keyword.";
}
else{
if (strlen($search)<=2) {
echo "Search term too short.";
}
else {
echo "You searched for <b>$search</b><hr size='1'>";
//connect to database
mysql_connect("localhost","root","");
mysql_select_db("searchengine");
//explode our search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each) {
//constuct query
$x++;
if ($x==1) {
$construct .= "keywords LIKE '%$search_each%'";
}
else {
$construct .= " OR keywords LIKE '%$search_each%'";
}
}
//echo out construct
$construct = "SELECT * FROM images WHERE $construct";
$run = mysql_query($construct) or die(mysql_error());
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "No results found.";
}
else {
echo "$foundnum results found!<p>";
while ($runrows = mysql_fetch_assoc($run)) {
//get data
$name = $runrows['name'];
$image = $runrows['image'];
echo "
<b>$name</b><br>
$image<br>
";
}
}
}
身体{
背景色:#FFF;
}
你应该用谷歌 您需要一个单独的php脚本来返回图像本身,然后需要从php脚本中调用它
另一个PHP脚本需要将内容类型头设置为正确的mime类型。您应该在另一个URL上创建另一个脚本,该脚本通过get参数接受ID以输出图像。然后可以按照以下思路做一些事情:
<?php
// ..your MySQL stuff
function error() {
echo "There was an error";
}
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$id = (int) $_GET['id'];
$sql = "SELECT * FROM images WHERE id = '$id'";
$query = mysql_query($sql, $conn);
if (mysql_num_rows() == 1) {
$data = mysql_fetch_assoc($query);
// Change this to the correct image type for your stored data
header("Content-type: image/gif");
echo $data['image'];
} else {
error();
}
} else {
error();
}
echo "<b>$name</b> <img src='theimagescript.php?id={$id}' alt='Image of $name' />";