php - 使用 PDO 执行高级搜索查询的问题

标签 php mysql pdo

我有以下代码,除了标题字段之外,所有搜索功能都可以工作。所以我可以按类型、日期、地点等搜索……但不能按标题搜索。当尝试按标题搜索时,根本不会返回任何内容。谁能帮我解决这个问题吗?

此外,是否有更有效的方法来计算所有字段,然后再将其限制用于稍后的分页?

$today = date("Y-m-d");
$query = "SELECT * FROM TABLE_NAME WHERE Date >= '$today'";

$bind = Array();

if ($_GET["Title"] && $_GET["Title"] != "") {
    $query .= " and Title like %?%";
    $bind['Title'] = $_GET['Title'];
}
if ($_GET["Genre"] && $_GET["Genre"] != "") {
    $query .= " and Genre like %?%";
    $bind['Genre'] = $_GET['Genre'];
}
if ($_GET["Location"] && $_GET["Location"] != "") {
    $query .= " and Location like %?%";
    $bind['Location'] = $_GET['Location'];
}
if ($_GET["Date"] && $_GET["Date"] != "") {
    $query .= " and Date = %?%";
    $bind['Date'] = $_GET['Date'];
}

$stmt = $db->prepare($query);
$stmt->execute($bind);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num = count($rows);

$query .= " ORDER BY Date LIMIT $limit, 9";
$stmt = $db->prepare($query);
$stmt->execute($bind);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

编辑:在大家的帮助下,我想我应该发布我现在修改后的代码以供将来引用。事实证明,其他字段不起作用,而是由于 if 语句,所有嵌套在代码中的内容根本没有被执行。

$today = date("Y-m-d");
$query = "SELECT * FROM TABLE_NAME WHERE Date >= '$today'";
$countq = "SELECT count(*) FROM TABLE_NAME WHERE Date >= '$today'";

$bind = Array();

if ($_GET["Title"] && $_GET["Title"] != "") {
    $query .= " and Title like :title";
    $countq .= " and Title like :title";
    $bind[':title'] = "%{$_GET['Title']}%";
}
if ($_GET["Genre"] && $_GET["Genre"] != "") {
    $query .= " and Genre like :genre";
    $countq .= " and Genre like :genre";
    $bind[':genre'] = "%{$_GET['Genre']}%";
}
if ($_GET["Location"] && $_GET["Location"] != "") {
    $query .= " and Location like :loc";
    $countq .= " and Location like :loc";
    $bind[':loc'] = "%{$_GET['Location']}%";
}
if ($_GET["Date"] && $_GET["Date"] != "") {
    $query .= " and Date = :date";
    $countq .= " and Date = :date";
    $bind[':date'] = "{$_GET['Date']}";
}

$stmt = $db->prepare($countq);
$stmt->execute($bind);
$rows = $stmt->fetchAll();
$num = count($rows);

$query .= " ORDER BY Date LIMIT $limit, 9";
$stmt = $db->prepare($query);
$stmt->execute($bind);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

最佳答案

all of the search functions work

对于给定的查询,它不正确

来自PDO tag wiki :

placeholders cannot represent an arbitrary part of the query, but a complete data literal only. Neither part of literal, nor whatever complex expression or a syntax keyword can be substituted with prepared statement.

Prepare FULL literal first: $name = "%$name%"; and then bind it.

至于“更”有效的分页方法 - 是的,哦,是的。
使用当前的数据计数方式,您实际上不需要其他查询。因为您已经拥有所有数据并且也可以对其进行分页。
但当然它很快就会污染所有的内存。因此,如果您想从数据库中获取行数,请获取具体计数:运行相同的查询,但不要使用 SELECT * 而是使用 "SELECT count(*)

There are not any errors returned, that's why I am so confused

再次来自 PDO 标签 wiki:

It is essential to set ERRMODE_EXCEPTION as a connection option as it will let PDO throw exceptions on connection errors. And this mode is the only reliable way to handle PDO errors.

关于php - 使用 PDO 执行高级搜索查询的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866185/

相关文章:

mysql - 使用C连接MYSQL

php - 令人困惑的仅 PDO 问题 : Can't connect through socket/Access denied/Can't connect to server (shared host)

php - 在 PHP 中使用 fetchAll 返回多个数组

javascript - Magento 重复加载 JS CSS 文件

mysql - 为什么包含TABLE和VIEW的SQL命令需要很长时间?

MySQL 在一个查询中使用多个联接来获取结果?

php - mySQL自增问题: Duplicate entry '4294967295' for key 1

javascript - 在 php 文件中执行 Google Chart API (Javascript)

php - 使用 javascript 或 PHP 如何使用下拉列表或文本框中的值。

php - 计算总条目数并在 Laravel 中制作一个数字列表