在 MySQL 查询的 LIMIT 子句中使用占位符时出现 PHP PDO 错误

标签 php mysql pdo

<分区>

$sql = "SELECT sql_calc_found_rows * FROM members".
       " ORDER BY username LIMIT :startRow, :numRows";

try {
    $st = $conn->prepare($sql);
    $st->bindParam(":startRow", $startRow, PDO::PARAM_INT);
    $st->bindParam(":numRows", $numRows, PDO::PARAM_INT);
    $st->execute();
} catch (PDOException $e) {
    die("Query failed: " . $e->getMessage());
}

这里出现错误:

Query failed: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ''5'' at line 1.

LIMIT :startRow, :numRows:numRows 中有问题。

我已经尝试了 $st->bindParam$st->bindValue,都没有用。

最佳答案

我认为问题出在 TBL_MEMBERS 我想这是一个 View (子选择)。 因此,如果您有产品表,并且您想执行以下语句:

select sql_calc_found_rows * from select id, code, name, slug, info from products order by code

您将收到以下错误:

SQL Error (1064): 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 'select id, code, name, slug, info from products order by code' at line 1

但是如果您将查询更改为:

select sql_calc_found_rows * from (select id, code, name, slug, info from products) v order by code

这会起作用。

总而言之, TBL_MEMBERS 是一个 View ,应该放在括号中并给定别名(我的示例别名是“v”)

关于在 MySQL 查询的 LIMIT 子句中使用占位符时出现 PHP PDO 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30389723/

相关文章:

php - Laravel maatwebsite excel包导入2000+行导致1390代码错误

php - 在php中访问关联数组中的第一个元素

mysql - 连接创建错误的结果查询

PHP PDO LIKE 语句不成功

php - 更改json_encode()输出结构

php - 如何使用 PDO 和 php 正确获取对象

php - 格式化存储在数据库中的日期

php - Mysql从多个表中选择,每个月创建一个新表

mysql - 加载数据本地 INFILe : loading data form text file auto incerment syntax

php - PHP分布式​​系统编程