2017-04-18 105 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