PHP分页问题和错误

时间:2015-12-04 01:56:24

标签: php pagination

我正在学习PHP。 我想让我的HTML页面分页。 这是我的代码:

我正在尝试每页制作6个视频。要做到这一点,我要搜索我有多少ID,这意味着我有相同的视频价值,所以我可以制作X页。

目前,我遇到了这个错误:

  

注意:mysqli_result类的对象无法在第20行的C:\ xampp \ htdocs \ Site \ index.php中转换为int

$stmt3 = $db->prepare ("SELECT COUNT(ID_Video) as total FROM Videos");

$stmt3->execute();

$result2 = $stmt3->get_result();
$result2->fetch_assoc();

// Remember to round it up always!
$VideosPerPage = 6;
$totalPages = ceil($result2 / $VideosPerPage);

// Check that the page number is set.
if(!isset($_GET['page'])){
    $_GET['page'] = 0;
}else{
    // Convert the page number to an integer
    $_GET['page'] = (int)$_GET['page'];
}

// If the page number is less than 1, make it 1.
if($_GET['page'] < 1){
    $_GET['page'] = 1;
    // Check that the page is below the last page
}else if($_GET['page'] > $totalPages){
    $_GET['page'] = $totalPages;
}

1 个答案:

答案 0 :(得分:0)

您看到此错误,因为$result2是PDO结果 - 不是整数。您需要从变量中的结果中分配获取的值。当您获得一列时,可以使用fetchColumn()。您的前几行可能如下所示:

$result2 = $stmt3->get_result();
$totalVideos = $result2->fetchColumn(); // will get the number from single column

// Remember to round it up always!
$VideosPerPage = 6;
$totalPages = ceil($totalVideos / $VideosPerPage);