PHP分页错误

时间:2014-02-26 14:24:30

标签: 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版本的问题,因为谷歌让我相信,或者我是否犯了语法错误..所以我已经包含了代码,以防存在错误。< / p>

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查询外,代码基本保持不变。

1 个答案:

答案 0 :(得分:0)

错误消息指出,

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];