Problem
First time working with pagination so I'm still trying to understand how this all fits together. I'm fairly confident that the method I have researched and applied here will work however I am encountering a strange error.
The data is not being accessed due to a failure of the second mysqli_query which incurs the error message Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given
.
There is only one entry in the database and I can only assume that this must be resulting in some strange logic but I'm not even 100% that it makes sense for that to be happening. It is also definitely the second mysqli_query as Could not get that data
is the output.
Would really appreciate some explanation of the process of pagination and if anyone can spot my mistake that would be greatly appreciated.
Code
<?php
$connect = mysqli_connect("localhost", "root", "");
if (!$connect) {die(mysql_error());}mysqli_select_db($connect, "story.time");
$rec_limit = 5;
$sql = "SELECT count(id) FROM toptable ";
$retval = mysqli_query($connect, $sql);
if(! $retval ){die('Could not get this data: ' . mysql_error());}
$row = mysqli_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ){$page = $_GET{'page'} + 1;$offset = $rec_limit * $page;}
else{$page = 0;$offset = 0;}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT id, name, content FROM toptable ORDER BY id DESC LIMIT $offset, $rec_limit";
$retval = mysqli_query($connect, $sql);
if(! $retval ){die('Could not get that data: ' . mysqli_error($retval));}
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{echo "{$row['id']} <br> "."{$row['name']} <br> "."{$row['content']} <br> ";}
if( $page > 0 ){$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";}
else if( $page == 0 )
{echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";}
else if( $left_rec < $rec_limit )
{$last = $page - 2;echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";}
mysqli_close($connect);
?>