2011-05-15 159 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' />"; 
相關問題