php - 带有 "WHERE title like %post%"的 MySQL 查询返回一个空结果集

标签 php mysql

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

select * from blogposts where title like %:term%

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

数据库.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) 不会从数据库返回任何内容。

搜索.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 网页。

最佳答案

改变这个:

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();
    }

关于php - 带有 "WHERE title like %post%"的 MySQL 查询返回一个空结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55537215/

相关文章:

php - 交响乐 3 : Select with relation many to many

php - 使 if 语句了解哪个 "time-left"格式更大

javascript - PHP Ajax 在文件中使用 jQuery 加载文件

php - 如何监控MySQL查询

Mysql,如何连接2个结果,欢迎提出想法

php - 在多个数据库中搜索的更快方法

javascript - 将选项 ID 值传递给 Controller ​​并通过 onclick 从另一个 Blade View 检索

php - 如何为 CentOS 6.5 版服务器设置 ffmpeg

mysql - 需要获取最近 10 个活跃主题的用户

php - MySql,限制每个类别的文章数量