使用分页代码的搜索结果不会返回结果

时间:2015-03-05 18:21:20

标签: php pdo

我很难通过分页代码获取此搜索结果。它成功地获取了在另一个页面上的html表单中输入的搜索关键字,并将其带入此search.php页面。如果我回显$ search,我会在页面上看到关键字。但即使我应该查询,我也没有得到任何结果。谁能看到可能会发生什么?

require "PDO_Pagination.php";

if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{

    $search = htmlspecialchars($_REQUEST["search_text"]);
    $pagination->param = "&search=$search";
    echo $search;

    $pagination->rowCount("SELECT * FROM stories WHERE stories.genre = $search");
    $pagination->config(3, 5);
    $sql = "SELECT * FROM stories WHERE stories.genre = $search ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
    $query = $connection->prepare($sql);
    $query->execute();
    $model = array();

    while($rows = $query->fetch())
    {
        $model[] = $rows;
    }
}
else
{
    $pagination->rowCount("SELECT * FROM stories");
    $pagination->config(3, 5);
    $sql = "SELECT * FROM stories ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
    $query = $connection->prepare($sql);
    $query->execute();
    $model = array();

    while($rows = $query->fetch())
    {
        $model[] = $rows;
    }
}

2 个答案:

答案 0 :(得分:0)

$query = "SELECT * FROM stories";

if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
    $search = htmlspecialchars($_REQUEST["search_text"]);
    $pagination->param = "&search=$search";
    $query .= " WHERE genre LIKE '%$search%'";
}
// No need for else statement.
$pagination->rowCount($query);
$pagination->config(3, 5);
$query .= " ORDER BY SID ASC LIMIT {$pagination->start_row}, {$pagination->max_rows}";

$stmt = $connection->prepare($query);
$stmt->execute();
$model = $stmt->fetchAll();
var_dump($model);

答案 1 :(得分:0)

在您的查询中执行:

WHERE stories.genre LIKE '%string%');

而不是:

WHERE stories.genre = 'string');

因为等于想要在字面上等于字段。