2015-04-03 80 views
0

我正在嘗試使用分頁功能每頁顯示3個項目。 但我得到這個錯誤PHP分頁時出現綁定值時出錯

Fatal error: Call to a member function bindParam() on boolean in C:\xampp\htdocs\cms\admin2\pagination.php on line 60 

我不得不尋找解決辦法,他們說主要是查詢的問題,導致錯誤,但我的SQL查詢是不是太複雜,使用的語法是正確的,但我不知道爲什麼錯誤仍然出來。

這裏是我的分頁代碼:

<?php 
include "dbConnection.php"; 

global $dbLink; 
try { 

    // Find out how many items are in the table 
    $total = $dbLink->query(' 
     SELECT 
      COUNT(*) 
     FROM 
      sponsor_item 
    ')->fetch_row()[0]; 

    // How many items to list per page 
    $limit = 3; 

    // How many pages will there be 
    $pages = ceil($total/$limit); 

    // What page are we currently on? 
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
     'options' => array(
      'default' => 1, 
      'min_range' => 1, 
     ), 
    ))); 

    // Calculate the offset for the query 
    $offset = ($page - 1) * $limit; 

    // Some information to display to the user 
    $start = $offset + 1; 
    $end = min(($offset + $limit), $total); 

    // The "back" link 
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>'; 

    // The "forward" link 
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>'; 

    // Display the paging information 
    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>'; 

    // Prepare the paged query 
    $stmt = $dbLink->prepare(' 
     SELECT 
      (*) 
     FROM 
      sponsor_item 
     ORDER BY 
      sponsor_item_id 
     LIMIT 
      :limit 
     OFFSET 
      :offset 
    '); 

    // Bind the query params 
    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); 
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT); 
    $stmt->execute(); 

    // Do we have any results? 
    if ($stmt->rowCount() > 0) { 
     // Define how we want to fetch the results 
     $stmt->setFetchMode(PDO::FETCH_ASSOC); 
     $iterator = new IteratorIterator($stmt); 

     // Display the results 
     foreach ($iterator as $row) { 
      echo '<p>', $row['sponsor_item_id'], '</p>'; 
     } 

    } else { 
     echo '<p>No results could be displayed.</p>'; 
    } 

} catch (Exception $e) { 
    echo '<p>', $e->getMessage(), '</p>'; 
} 
?> 

我做了什麼不對的分頁頁面上?請指出我的錯誤。謝謝

回答

-1

很確定您的查詢中有錯別字,您的SELECT條款中的(*)應該只是*

+0

您好,謝謝您的回覆。即使我把(*)或*,兩者也不能工作 – 2015-04-03 07:28:39

+0

它仍然給我同樣的錯誤 – 2015-04-03 07:28:58

-1

$ stmt是一個布爾值,因爲prepare()失敗。所以你需要習慣使用防禦性編碼實踐。

// Prepare the paged query 
$stmt = $dbLink->prepare(' 
    SELECT 
     (*) 
    FROM 
     sponsor_item 
    ORDER BY 
     sponsor_item_id 
    LIMIT 
     :limit 
    OFFSET 
     :offset 
'); 

if($stmt === false){ 
    // handle the error here 
} 
+0

嗨,那裏,我的查詢有什麼問題嗎?爲什麼它會返回錯誤?我輸入正確的表名和訂單名稱。難道它不是假設查詢正確嗎? – 2015-04-03 07:34:15

+0

您是否檢查過可從pdo/mysqli獲得的錯誤?我們在這裏幫助你瞭解自己認識的東西,所以先試着幫助你自己。我指出你應該在哪裏檢查錯誤。閱讀maual並檢查錯誤。那麼你會得到你的問題的答案。 – 2015-04-03 13:29:53