【发布时间】:2013-03-29 00:14:24
【问题描述】:
我有一个 pdo 语句来选择行并将它们返回到一个数组中。我正在使用分页显示每页 12 个结果。如果我直接输入 LIMIT 12, 12 ,而不是 LIMIT ?, ? ,则该语句可以正常工作。
我对这两个变量的 bindParam 做错了什么?
两个变量都包含正确的数据。
这是我正在使用的功能:
// Retrieve active posts
function retrieve_active_posts(){
//test the connection
try{
//connect to the database
$dbh = new PDO("mysql:host=db2940.oneandone.co.uk;dbname=db348699391","xxx", "xxx");
//if there is an error catch it here
} catch( PDOException $e ) {
//display the error
echo $e->getMessage();
}
// Get all the posts
$stmt = $dbh->prepare(" SELECT p.post_id, post_year, post_desc, post_title, post_date, img_file_name, p.cat_id
FROM mjbox_posts p
JOIN mjbox_images i
ON i.post_id = p.post_id
AND i.cat_id = p.cat_id
AND i.img_is_thumb = 1
AND post_active = 1
ORDER BY post_date
DESC
LIMIT ?,?");
$stmt->bindParam(1, $limit);
$stmt->bindParam(2, $offset);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$resultarray[] = $row;
}
return $resultarray;
}
非常感谢
【问题讨论】:
-
可以添加
var_dump($limit, $offset);的输出吗? -
@hek2mgl NULL 两者都为NULL
-
@crm 检查我的答案 ;)
-
顺便说一句,当 PDO 为您提供
fetchAll()时,为什么要使用fetch()循环遍历结果集以构建数组? -
另外,我认为您的参数顺序错误。首先是偏移量,然后是要返回的记录数(除非其中包含
OFFSET关键字,在这种情况下顺序颠倒)。
标签: php mysql select binding pdo