LIMIT 和 OFFSET 过滤数据库结果的问题(语法错误)
Problems with LIMIT and OFFSET to filter database results (error in syntax)
我正在尝试创建一个页面,用户可以在其中搜索书籍,他们可以输入书名和作者 - 这是可选的,还可以输入返回列表的起始位置和列表的长度。然后应返回包含详细信息的书籍列表。
当我尝试 运行 使用 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>";
我不确定错误是什么我如何让这段代码工作?
我希望这个方法是正确的,因为我无法测试它。
您应该只使用 LIMIT
:
$sql = "SELECT title, authors, description, price
FROM book2
WHERE title LIKE '$title%'
AND author LIKE '%$authors%'
LIMIT $start,$length";
OFFSET
的语法仅出于兼容性原因:
For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.
LIMIT
的有效示例如下:
LIMIT offset, row_count
LIMIT row_count
LIMIT row_count OFFSET offset
$sql = "SELECT title, authors, description, price
FROM book2
WHERE title LIKE '$title%'
AND author LIKE '%$authors%'
顶行显示 authors
,底部显示 author
。由于提供的错误,我会说 author
是正确的,你应该删除 authors
中的 s
虽然检查了 cascaval 关于限制的答案。修复 author
后,您的代码中可能会出现第二个错误,而 cascavals 的答案应该可以帮助您解决这个问题。
我正在尝试创建一个页面,用户可以在其中搜索书籍,他们可以输入书名和作者 - 这是可选的,还可以输入返回列表的起始位置和列表的长度。然后应返回包含详细信息的书籍列表。
当我尝试 运行 使用 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>";
我不确定错误是什么我如何让这段代码工作? 我希望这个方法是正确的,因为我无法测试它。
您应该只使用 LIMIT
:
$sql = "SELECT title, authors, description, price
FROM book2
WHERE title LIKE '$title%'
AND author LIKE '%$authors%'
LIMIT $start,$length";
OFFSET
的语法仅出于兼容性原因:
For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.
LIMIT
的有效示例如下:
LIMIT offset, row_count
LIMIT row_count
LIMIT row_count OFFSET offset
$sql = "SELECT title, authors, description, price
FROM book2
WHERE title LIKE '$title%'
AND author LIKE '%$authors%'
顶行显示 authors
,底部显示 author
。由于提供的错误,我会说 author
是正确的,你应该删除 authors
虽然检查了 cascaval 关于限制的答案。修复 author
后,您的代码中可能会出现第二个错误,而 cascavals 的答案应该可以帮助您解决这个问题。