PHP分页错误

标签 php mysql pdo pagination

我正在尝试将分页集成到我的代码中,但我收到了错误 Fatal error: Call to undefined method mysqli_result::fetchColumn() 对应于代码行: $total = $connection->query('SELECT COUNT(*) FROM table_shift INNER JOIN table_staff ON table_shift.uniqueid = table_staff.uniqueid')->fetchColumn();。我正在使用 PHP 5.4.12 版运行 wampserver。我不知道这是否是我的 PHP 版本的问题,因为谷歌让我相信,或者我是否犯了语法错误。所以我已经包含了代码以防其中存在错误。

try {

// Find out how many items are in the table
$total = $connection->query('SELECT COUNT(*) FROM table_shift INNER JOIN table_staff ON table_shift.uniqueid = table_staff.uniqueid')->fetchColumn();

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

// 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 = $connection->prepare('SELECT * FROM table_shift INNER JOIN table_staff ON table_shift.uniqueid = table_staff.uniqueid ORDER BY shiftid DESC 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 "<td>". htmlentities($row['shiftid'], ENT_QUOTES, 'UTF-8') . "</td> "; 
     echo "<td>". htmlentities($row['first_name'], ENT_QUOTES, 'UTF-8') . " " . htmlentities($row['surname'], ENT_QUOTES, 'UTF-8') . "</td> ";
     if($row['location']==0) 
         { echo "<td>Location 1</td> ";} 
     else 
         { echo "<td>Location 2</td> ";}
     echo "<td>". htmlentities(date('d-m-Y', strtotime($row['shift_date'])), ENT_QUOTES, 'UTF-8') . "</td> ";
     echo "<td>". htmlentities($row['start_time'], ENT_QUOTES, 'UTF-8') . "</td> ";
     echo "<td>". htmlentities($row['end_time'], ENT_QUOTES, 'UTF-8') . "</td> ";
     echo "<td>". htmlentities($row['total_hours'], ENT_QUOTES, 'UTF-8') . "</td> ";
     echo "<td>£". htmlentities(number_format($row['totalPaid'], 2, '.', ','), ENT_QUOTES, 'UTF-8') ."</td> ";
    }

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

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

除了将 $dbh 更改为 $connection 以及为我的目的更新 SQL 查询外,代码基本保持不变。

最佳答案

mysqli_result 没有 fetchColumn 方法,如错误消息所述。查看文档以获得更多帮助:http://uk1.php.net/mysqli-result

您可以改用 fetch_row:

$total = $connection->query('SELECT COUNT(*) FROM table_shift INNER JOIN table_staff ON table_shift.uniqueid = table_staff.uniqueid')
->fetch_row()[0];

关于PHP分页错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22044279/

相关文章:

php - 变量内容的简单转储

php pdo : get the columns name of a table

CentOS 6.5 上的 PHP MSSQL 5.5.8-1?

php - php文件中的8个空格如何使apache崩溃?

php - 检测 PHP 解析多维 POST 数据期间的错误

php - 检测文章中的趋势 "reactions"(一个或多个)(如 Buzzfeed 等)

mysql - 选择给定条件的数据 MySQL

php - 比较运算符 Sql 查询与 php

php - 获取两种数据并显示出来

php - 带有 pdo 的基于文件的 php 数据库