简体   繁体   English

为什么这些参数在这个Mysql查询中没有经过?

[英]Why don't these parameters go through in this Mysql query?

What is wrong with this query? 这个查询有什么问题?

I can't get this query to pass these arguments! 我无法通过这些参数获得此查询!

$offset = 5;
$rowsperpage = 10;
$stmt = $db->prepare("SELECT * FROM table ORDER BY ID DESC LIMIT ?,?");
if ($stmt->execute(array($offset, $rowsperpage))) {
  while ($row = $stmt->fetch()) {
    echo $row['title'];
  }
}

If I change the query to this it works fine, but I need to pass the strings because they are dynamic. 如果我将查询更改为此工作正常,但我需要传递字符串,因为它们是动态的。

$stmt = $db->prepare("SELECT * FROM table ORDER BY ID DESC LIMIT 5,10");
if ($stmt->execute(array($offset, $rowsperpage))) {
  while ($row = $stmt->fetch()) {
    echo $row['title'];
  }
}

I have a feeling that "lazy execution" treats all parameters as strings and thus enclose them in quotes. 我有一种感觉,“延迟执行”将所有参数视为字符串,因此将它们括在引号中。

So, either bind your parameters explicitly, using bind_param instead of passing array into execute() 所以,要么显式绑定你的参数,使用bind_param而不是将数组传递给execute()

or set emulation mode to off 或将仿真模式设置为关闭

$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

right after connect. 在连接之后。

anyway, you have to get in touch with the error message first. 无论如何,您必须先与错误消息联系。
So, setting this one 所以,设置这一个

$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

will reveal the error message to you 将向您显示错误消息

你忘了做bind_param来填补空白

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM