如何显示数据库中的图像? (PHP)

时间:2011-05-15 14:35:03

标签: php search-engine

我在我的数据库中设置了一个图像表,将我的图像存储为blob类型。我的问题是我不知道如何从我的网页搜索页面显示数据库中的图像。无论何时我输入一个搜索查询,它都会显示关键字&图像名称但它不会显示图像本身。而是显示长sql代码。

以下是我对Imagesearch.php;

的php代码
<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>

                                    ";
                            }
                    }
    }

}

3 个答案:

答案 0 :(得分:0)

您应该使用Google ... Link

答案 1 :(得分:0)

您需要一个单独的PHP脚本来返回图像本身,然后您需要从PHP脚本中调用它。

另一个PHP脚本需要将Content-type标头设置为正确的mime类型。

答案 2 :(得分:0)

您应该在另一个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' />";