2017-04-18 102 views
-1

根據搜索模式,我需要從服務器獲取顯示的數據。根據文本區域值選擇行

include("dbconfig.php"); 
$sql="select * from blog where title LIKE '{$title}%'"; 
$res=mysql_query($sql); 
while($row=mysql_fetch_array($res)) 
{ 
    echo"<tr>"; 
     echo"<td><img src='uploads/".$row['file']."' height='150px' width='200px'</td>"; 
     echo"<td><h3>".$row['title']."</h3>".$row['description']."</td>"; 
    echo"</tr>"; 
} 
+0

\t \t \t \t
swapnika

+0

那麼,你面臨的問題是什麼? –

+0

@swapnika有什麼問題? –

回答

0

這裏是實現mysqli的問題作爲下一個評論完全重寫。爲了安全性&易於使用,它使用prepared statementbound parameterbound results

(另請注意,我把它換成你的SELECT的*通配符,它​​始終是很好的做法,只要求在數據庫中,你所需要的東西。)

$db=new mysqli("localhost","username", "password","database"); // do this in your include 
if($stmt=$db->prepare("SELECT `file`,`title`,`description` FROM `blog` WHERE `title` LIKE ?")){ 
    $search="{$_GET['title']}%"; // I assume this is passed with $_GET 
    $stmt->bind_param("s",$search); 
    $stmt->execute(); 
    $stmt->bind_result($file,$title,$description); 
    while($stmt->fetch()){ 
     echo"<tr>"; 
      echo"<td><img src='uploads/{$file}' height='150px' width='200px'</td>"; 
      echo"<td><h3>{$title}</h3>{$description}</td>"; 
     echo"</tr>"; 
    } 
    $stmt->close(); 
} 

附:通常,通過在LIKE值的兩側使用%來完成表格搜索。您的搜索將只返回「從title開始」的結果。請考慮在你的代碼中改變它。

0

更改查詢象下面這樣:

$sql="select * from blog where title LIKE '".$title."%'; 
+0

你期待'%'做什麼?你的意思是把我放在'''''('... LIKE'「。$ title。」%'') –

+0

是的你是對的 – lalithkumar