php PDO 限制和偏移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5508993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO LIMIT and OFFSET
提问by proyb2
Possible Duplicate:
PHP PDO bindValue in LIMIT
I could not display the data when using LIMIT and/or OFFSET in the prepare statement, but I can show "Lei Lei" if I don't use the LIMIT and OFFSET, does the code look wrong?
在prepare语句中使用LIMIT和/或OFFSET时无法显示数据,但是如果我不使用LIMIT和OFFSET,我可以显示“雷磊”,代码看起来有问题吗?
$statement = $conn->prepare("SELECT id,username FROM public2 WHERE username = :name LIMIT :sta OFFSET :ppage");
$name = "Lei Lei";
$statement->execute(array(':name' => $name,':sta' => $start,':ppage' => $per_page));
This have been change from the original code which worked:
这已经从原来的代码改变了:
$query_pag_data = "SELECT id,username from public2 LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
回答by proyb2
I found the answer!
我找到了答案!
$statement->bindValue(':sta1', (int) $start, PDO::PARAM_INT);
does work
确实有效
回答by Your Common Sense
$dbh->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
will let you bind variables without being bothered of them type
会让你绑定变量而不会被它们的类型打扰
回答by chriso
Edit: Fixed
编辑:固定
$statement = $conn->prepare("SELECT id,username FROM public2 WHERE username = :name LIMIT :limit OFFSET :offset");
$name = "Lei Lei";
$statement->bindValue(':name', $name);
$statement->bindValue(':limit', (int) $start, PDO::PARAM_INT);
$statement->bindValue(':offset', (int) $per_page, PDO::PARAM_INT);
$statement->execute();