【问题标题】:Select rows based on textarea value根据 textarea 值选择行
【发布时间】:2017-04-18 12:16:46
【问题描述】:

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

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

【问题讨论】:

  • 所以?您面临的问题是什么?
  • @swapnika 有什么问题?
  • 停止使用已弃用的mysql_* API。使用mysqli_*PDO
  • mysql_* 在 PHP 7 中已被弃用。尝试使用 PDO 或 mysqli_*

标签: php mysql select sql-like


【解决方案1】:

这是一个完整的重写,它实现了问题下评论的 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 开头”的结果。请考虑在您的代码中更改此设置。

【讨论】:

    【解决方案2】:

    如下更改查询:

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

    【讨论】:

    • 您希望% 做什么?你的意思是把 i 放在"' 之间吗? (...LIKE '".$title."%')
    猜你喜欢
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2014-01-23
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多