【问题标题】:PHP Pagination issues and errorsPHP分页问题和错误
【发布时间】:2015-12-04 10:15:31
【问题描述】:

我现在正在学习 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;
}

【问题讨论】:

    标签: php pagination


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多