具有“ WHERE标题,如%post%”的MySQL查询返回空结果集

时间:2019-04-05 14:06:38

标签: php mysql

我已经开始用PHP编写内容管理系统。我需要对数据库表运行以下SQL查询:

select * from blogposts where title like %:term%

但是,此查询返回空结果集,并且没有数据被打印到PHP网页。这是我的代码:

Database.php:

<?php

class Database {

    protected $connection = null;
    protected $statement = null;

    public function openConnection($username, $password) {
        try {
            $this->connection = new PDO("mysql:host=localhost;dbname=content_management_system", $username, $password);
        } catch (Exception $e) {
            die($e);
        }
        $this->connection->exec("SET CHARACTER SET UTF-8");
    }

    public function getResultSet() {
        return $this->statement->fetch();
    }

}

?>

BlogPosts.php:

<?php

include('./Database.php');

class BlogPosts extends Database {

    function __construct() {
        $this->openConnection("stack", "overflow");
    }

    public function getBlogPostsWithTag($tag) {
        $sql = "select * from blogposts where tag = :tag";
        $this->statement = $this->connection->prepare($sql);
        $this->statement->bindParam(":tag", $tag);
        $this->statement->execute();
    }

    public function getBlogPostsWithName($term) {
        $sql = "select * from blogposts where title like %:term%";
        $this->statement = $this->connection->prepare($sql);
        $this->statement->bindParam(":term", $term);
        $this->statement->execute();
    }

}

?>

为了清楚起见,getBlogPostsWithTag()可以正常工作,但是与此非常相似的getBlogPostsWithName($term)不会从数据库返回任何内容。

search.php:

<?php
    include("./BlogPosts.php");
    if (isset($_REQUEST["search"])) {
        $blogpostsTerm = new BlogPosts();
        $blogpostsTerm->getBlogPostsWithName($_REQUEST["term"]);
        while ($resultset = $blogpostsTerm->getResultSet()) {
            echo "<div class='blog-post'>";
            echo "<h2 class='blog-post-title'>" . $resultset["title"] . "</h2>";
            echo "<p class='blog-post-meta'>" . $resultset["created_at"] . " by <a href='#'>" . $resultset["author"] . "</a></p>";
            echo $resultset["lede"];
            echo "</div>";
        }
    }
?>

我期望方法getBlogPostsWithName($term)从数据库表中返回一些数据,但是实际结果是没有任何内容被返回/回显到PHP网页。

3 个答案:

答案 0 :(得分:3)

更改此:

public function getBlogPostsWithName($term) {
        $sql = "select * from blogposts where title like %:term%";
        $this->statement = $this->connection->prepare($sql);
        $this->statement->bindParam(":term", $term);
        $this->statement->execute();
    }

对此:

public function getBlogPostsWithName($term) {
        $sql = "select * from blogposts where title like :term";
        $this->statement = $this->connection->prepare($sql);
        $bindingTerm = "%$term%";
        $this->statement->bindParam(":term", $bindingTerm);
        $this->statement->execute();
    }

答案 1 :(得分:1)

占位符必须独立存在。在变量上连接通配符,或者在SQL中将%与占位符连接起来。

$sql = "select * from blogposts where title like :term";
$this->statement = $this->connection->prepare($sql);
$this->statement->bindParam(":term", $term);
$term =  '%' . $term . '%';

$sql = "select * from blogposts where title like concat('%', :term, '%')";
$this->statement = $this->connection->prepare($sql);
$this->statement->bindParam(":term", $term);

或者您可以使用https://www.php.net/manual/en/pdostatement.bindvalue.php

$sql = "select * from blogposts where title like :term";
$this->statement = $this->connection->prepare($sql);
$this->statement->bindValue(":term", '%' . $term . '%');

答案 2 :(得分:0)

正如上面其他人所提到的,您应该在参数内包含%个字符。

$this->statement->bindParam("term", "%" . $term . "%");

背后的原因是通配符应该是字符串的一部分。但是,您的结果等于like %"blabla"%,这是错误的。

而且bindParam()中不需要冒号