根据textarea值选择行 [英] Select rows based on textarea value
问题描述
根据搜索模式,我需要从服务器获取显示的数据.
Depending on the search pattern I need to get the data displayed from the server.
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>";
}
推荐答案
此处是对mysqli的完整重写,如问题所述.为了安全&易于使用,它使用准备的语句,并带有绑定参数和
Here is a complete rewrite that implements mysqli as commented under the question. For security & ease of use, it uses a prepared statement with a bound parameter and bound results.
(还要注意,我已经在您的SELECT中替换了*
通配符.总是只向数据库询问您到底需要什么是一种好习惯.)
(Also notice, I've replaced the *
wildcard in your SELECT. It is always good practice to only ask the database for exactly what you need.)
$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
开头"的结果.请考虑在您的代码中进行更改.
p.s. Typically table searches are done by using %
on both sides of your LIKE
value. Your search will only return results that "start with title
". Please consider changing this in your code.
这篇关于根据textarea值选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!