根据textarea值

时间:2017-04-18 12:16:47

标签: php mysql select sql-like

根据搜索模式,我需要从服务器获取数据。

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>";
}

2 个答案:

答案 0 :(得分:0)

更改下面的查询:

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

答案 1 :(得分:0)

这是一个完整的重写,在问题下评论实现了mysqli。为安全起见易于使用,它使用prepared statement bound 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();
}

P.S。通常,通过在%值的两侧使用LIKE来完成表格搜索。您的搜索只会返回以title&#34;开头的结果。请考虑在代码中更改此内容。