PHP分页问题和错误


PHP Pagination issues and errors

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

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

目前,我有一个错误:

注意:在第20行的C:''examplep''htdocs''Site''index.php中,类mysqli_result的对象无法转换为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;
}

您看到此错误是因为$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);