简体   繁体   中英

PHP Pagination issues and errors

I'm learning PHP at the moment. I want to make my HTML page pagination. Here is the code I have:

I'm trying to make 6 videos only per page. And to do that I'm going to search how many ID's I have it means I have that same value of videos so I can make X pages.

At the moment, I'm having this error:

Notice: Object of class mysqli_result could not be converted to int in C:\\xampp\\htdocs\\Site\\index.php on line 20

$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;
}

You are seeing this error because $result2 is PDO result- not an integer. You need to assign the fetched value from the result in a variable. You can use fetchColumn() as you are getting one column. Your first few lines could look like following:

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM