PDO bindParam不允许语句返回结果 [英] PDO bindParam not allowing statement to return results

查看:103
本文介绍了PDO bindParam不允许语句返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDO语句来选择行和数组归还。我使用分页显示每页12名的成绩。如果我直接输入 LIMIT 12,12 ,而不是限制?,?语句正常工作。

I have a pdo statement to select rows and return them in an array. I am using pagination to display 12 results per page. If I directly input LIMIT 12, 12 , instead of LIMIT ?, ? the statement works correctly.

我在做什么毛病bindParam两个变量?

What am I doing wrong with the bindParam for both variables?

这两个变量包含正确的数据。

Both variables contain the correct data.

继承人我使用的功能:

// 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;
}

非常感谢

推荐答案

确保 $上限和和 $抵消被设定为适当的整数值。如果这保证了code应该工作

Make sure that $limit and and $offset are set to proper integer values. If this is ensured the code should work

想象一下,这个例子是使用哈日codeD值,会给你的第20条记录:

Imagine this example which is using harcoded values and will give you the first 20 records:

$limit = 20;
$offset = 0;

$stmt->bindParam(1, $limit,  PDO::PARAM_INT);
$stmt->bindParam(2, $offset, PDO::PARAM_INT);

这篇关于PDO bindParam不允许语句返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆