2013-04-06 72 views
0

我有一個pdo語句來選擇行並在數組中返回它們。我正在使用分頁顯示每頁12個結果。如果我直接輸入LIMIT 12, 12,而不是LIMIT ?, ?該聲明正常工作。PDO bindParam不允許語句返回結果

我在爲兩個變量的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; 
} 

非常感謝

+0

你可以添加的var_dump'輸出($極限,$偏移) ;'? – hek2mgl 2013-04-06 16:33:44

+0

@ hek2mgl NULL NULL NULL – crm 2013-04-06 16:35:58

+0

@crm檢查我的答案;) – hek2mgl 2013-04-06 16:37:34

回答

2

確保$limit和和$offset被設置爲正確的整數值。如果這保證了代碼應工作

想象一下這樣的例子是使用harcoded值,並給你的前20條記錄:

$limit = 20; 
$offset = 0; 

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

我完全按照你寫的那樣來試驗你的例子,並且我得到這個錯誤'致命錯誤:不能通過引用傳遞參數2' – crm 2013-04-06 16:47:16

+0

真的嗎? (不敢相信)。嘗試使用變量更新的示例 – hek2mgl 2013-04-06 16:49:41

+0

剛剛嘗試過,並且具有相同的致命錯誤。 – crm 2013-04-06 16:51:31