2

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

?>
davejal
  • 6,009
  • 10
  • 39
  • 82
masteryupa
  • 101
  • 13
  • I like your willpower, but why not use a class or script that already has all these elements by default? Do you need a wheel that has never been invented before? – davejal Dec 16 '15 at 00:44
  • @davejal It's more of a exercise in learning but I'd be happy to employ a different method if it helped. Would you recommend anything? – masteryupa Dec 16 '15 at 00:48
  • If it's an exercise continue (best way to learn). There are plenty I can't really suggest any, but I have been using a [framework](http://laravel.com/) for some time now. This also teaches you to write OOP programs, instead of procedural. – davejal Dec 16 '15 at 00:51
  • And while you're at it check out [this](http://stackoverflow.com/q/845021/3664960) to debug your php code. – davejal Dec 16 '15 at 00:55
  • @davejal Well that was pretty much why I came here because otherwise I'll be stuck doing it myself forever. This looks really nice and I'll investigate it more another time, thanks. – masteryupa Dec 16 '15 at 00:55
  • SO is not here to suggest any tutorial/framework or tool, so sorry, I can't really suggest anything. But to help further with this error, could you add the line which produces the first error? – davejal Dec 16 '15 at 00:58
  • @davejal No worries. So the error occurs during this section `$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));}` – masteryupa Dec 16 '15 at 01:01
  • @davejal I should also say, I've tried removed $offset and $rec_limit and the outcome is the same. – masteryupa Dec 16 '15 at 01:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98044/discussion-between-davejal-and-masteryupa). – davejal Dec 16 '15 at 01:06
  • added some comments in chat... check it out – davejal Dec 16 '15 at 01:12

1 Answers1

0
if (!$connect) {die(mysql_error());}mysqli_select_db($connect, "story.time");

mysql_error()

you're using mysqli elsewhere. Try to stick to one.

prats1411
  • 162
  • 12