【问题标题】:Problems with LIMIT and OFFSET to filter database results (error in syntax)使用 LIMIT 和 OFFSET 过滤数据库结果的问题(语法错误)
【发布时间】:2015-04-21 14:21:58
【问题描述】:

我正在尝试创建一个页面,用户可以在其中搜索一本书,他们输入标题和作者 - 这是可选的,并且还输入返回的列表应该从哪里开始以及列表的长度。然后应返回包含详细信息的书籍列表。

当我尝试使用 print_r($stmt->errorInfo()); 运行代码时,我得到了错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' authors = '' LIMIT '2' OFFSET '0'' at line 1 )

这里是主要代码:

$title = $_GET["title"];
$authors = $_GET["authors"];
$start = (int)$_GET["start"];
$length = (int)$_GET["length"];

$sql = "SELECT title, authors, description, price
FROM book2
WHERE title LIKE '$title%' 
AND author LIKE '%$authors%'
OFFSET 0,$start
LIMIT 0,$length";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':authors', $authors);
$stmt->bindParam(':length', $_GET['length'], PDO::PARAM_INT);
$stmt->bindParam(':start', $_GET['start'], PDO::PARAM_INT);

     $stmt->execute(array(

    ':title' => $title,
    ':authors' => $authors,
    ':start' => $start,
     ':length' => $length   
      ));

print_r($stmt->errorInfo());

echo "<table>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];

echo "<tr>";
        echo "<td>Title</td>";
        echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
        echo "<td>Authors</td>";
        echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
        echo "<td>Description</td>";
        echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
        echo "<td>Price</td>";
        echo "<td>$price</td>";
echo "</tr>";
}
echo "</table>";

我不确定错误是什么我如何让这段代码工作? 我希望方法是正确的,因为我无法测试它。

【问题讨论】:

  • 错误消息中显示的 SQL 与脚本中正在构建的 SQL 不匹配。可以显示var_dump($sql) 输出吗?
  • @MikeBrant 这是一个准备好的声明。因此输出 $sql 将无济于事,因为他看不到 mysql 使用它的语句,只有他的预准备版本。
  • @DocRattie 是的。我明白那个。仍然看不到 OP 如何以脚本中显示的准备好的语句 SQL 的错误消息中显示的 SQL 结束。
  • ` ':authors' => $authors,` 将authors 添加到 where 子句中,即使它不是之前的参数。但是他的数据库中没有authors,因此他得到了错误。

标签: php mysql pdo limit offset


【解决方案1】:

您应该只使用LIMIT

$sql = "SELECT title, authors, description, price
FROM book2
WHERE title LIKE '$title%' 
AND author LIKE '%$authors%'
LIMIT $start,$length";

OFFSET 的语法仅出于兼容性原因:

为了与 PostgreSQL 兼容,MySQL 还支持 LIMIT row_count OFFSET 偏移语法。

LIMIT 的有效示例如下:

LIMIT offset, row_count
LIMIT row_count
LIMIT row_count OFFSET offset

【讨论】:

  • 就是这样,谢谢,我无法弄清楚语法。
【解决方案2】:
$sql = "SELECT title, authors, description, price
FROM book2
WHERE title LIKE '$title%' 
AND author LIKE '%$authors%'

在顶行显示authors,在底部显示author。由于提供的错误,我会说 author 是正确的,您应该删除 authors 中的 s

尽管检查 cascaval 关于限制的答案。修复 author 后,您的代码中可能会出现第二个错误,而 cascavals 的答案应该可以帮助您解决这个问题。

【讨论】:

  • 天哪,是的,我没有发现这个。请注意,它仍然没有解决问题 - 错误返回相同。
  • 你应该检查你关于authors的嚎叫代码。你多次使用它。修复所有这些并检查您是否收到不同的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 2020-08-11
  • 2019-08-28
  • 1970-01-01
相关资源
最近更新 更多