2011-05-15 114 views
0

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

这是我的php代码Imagesearch.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> 

            "; 
          } 
        } 
    } 

}

回答

0

你应该使用谷歌... Link

0

你需要一个单独的PHP脚本,将返回图像本身,然后你将需要提醒的是你的PHP脚本。

其他PHP脚本需要将Content-type标头设置为适当的MIME类型。

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' />"; 
相关问题