php - PDO 参数读取为字符串而不是 int

标签 php mysql pdo

<分区>

我想将 GET var 传递到查询中以设置 OFFSET。

try {

  $sql_offset =  isset($_GET['offset']) ? $_GET['offset'] : 0;

  $main_query = "SELECT * FROM Orders
                LIMIT 150 OFFSET :offset";

  $result = $db->prepare($main_query);
  $result->bindParam(':offset', $sql_offset, PDO::PARAM_INT);
  $result->execute();

} catch (Exception $e) {

  $error = $e->getMessage();
  echo "<h2>".$error."</h2>";
  die();

}

查询失败,收到的错误信息为:

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 ''450'' at line 2

注意:如果我将 450 直接放入查询中,它就可以正常工作。

在我看来,问题在于它将参数读取为字符串“450”而不是 int 450。我尝试用 PDO::PARAM_INT 解决这个问题 - 没有帮助。
我用 (int)$sql_offsetint_val($sql_offset) 解析了值,但没有帮助。意思是,我仍然收到相同的错误消息。

问题:我说得对吗?它被视为字符串的问题是什么?如果是这样,我该如何解决?
还是这里有其他问题?

最佳答案

尝试解析 int 中的值,然后检查:

$sql_offset =  (int)(isset($_GET['offset']) ? $_GET['offset'] : 0);

关于php - PDO 参数读取为字符串而不是 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692703/

相关文章:

php - ORDER BY number_column DESC : why does a smaller number come before a larger?

php - 即使文件存在,使用 dirname(__FILE__) 附加样式表也不起作用

php - 数据表过滤器显示重复行

php - 错误: Object of class PDO could not be converted to string

php - MySQL:从表中获取最后更新ID以将其插入到另一个表中

PHP PDO 准备语句缓存

php - 从数组中删除重复值

php - Symfony2 验收测试不起作用

sql - 维基百科图形数据库插入

php - 如何在 PHP 中更改 PDO/SQLite 连接的字符编码?