Select 行基于 textarea 值

Select rows based on textarea value

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

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

像下面这样更改查询:

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

这是一个完整的重写,实现了问题下评论的 mysqli。为了安全性和易用性,它使用 prepared statement with a bound parameter and bound 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。通常 table 搜索是通过在 LIKE 值的两边使用 % 来完成的。您的搜索只会 return 结果 "start with title"。请考虑在您的代码中进行更改。