Viewing MySql records into pages

I'm using PHP4 with MySql and I want to view a query result into pages each page with 10 records max. so can anyone help me to do that?

My psaudo code is:


while ($row=mysql_fetch_array($result))
?> <!-- End of PHP code -->


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, I provided a complete working example in this question.  Just cut-n-paste, try it out, then modify it for your own database tables and columns...

yes, thats it. Try the link...nothing more to add
ChampionAuthor Commented:
Thanks... I used that example but because im using FORM in my example and the values from that FORM will be missed after doing that!
so how I can make that without missing the form data (Remember im using POST method).

- Champion
Here you go:

    mysql_connect("localhost", "testuser", "test")
         or die("(".mysql_errno().") Error:".mysql_error());
    mysql_select_db( "test3" )
         or die("(".mysql_errno().") Error:".mysql_error());
<TITLE>Example 1</TITLE>

Here is the example:

    $items_per_page = 3;

    if (isset($_POST['PREV']) && isset($_POST['startprev'])) {
      $first = $_POST['startprev'];
    } else if (isset($_POST['NEXT']) && isset($_POST['startnext'])) {
      $first = $_POST['startnext'];
    } else {
      $first = 0;

    $sql = "SELECT * FROM names_table";
    $result = mysql_query($sql) or die("(".mysql_errno().") Error:".mysql_error());
    $total_rows = mysql_num_rows($result);

    $sql = "SELECT * FROM names_table ORDER BY lastname LIMIT " . $first . ", " . $items_per_page;
    $result = mysql_query($sql) or die("(".mysql_errno().") Error:".mysql_error());

    $trow = 0;
    print "<table>"."\n";
    while ($row = mysql_fetch_array($result)) {
       if ($trow=='0') {
        print "<TH>last</TH><TH>first</TH><TH>ID</TH>";
       print "<TR>";
       print "<TD>".$row['lastname']."</TD><TD>".$row['firstname']."</TD><TD>".$row['id']."</TD>";
       print "</TR>";
    print "</TABLE>";
    print "<P>";
    print "<FORM ACTION='test.php' METHOD='POST'>";
    if ($first != '0') {
     $start = $first - $items_per_page;
        print "<INPUT TYPE='HIDDEN' NAME='startprev' VALUE='" . $start . "'>";
        print "<INPUT TYPE='SUBMIT' NAME='PREV' VALUE='Previous'>";
    $endrow = $first + $trow;
    if ($endrow < $total_rows) {
     $start = $first + $items_per_page;
        print "<INPUT TYPE='HIDDEN' NAME='startnext' VALUE='" . $start . "'>";
        print "<INPUT TYPE='SUBMIT' NAME='NEXT' VALUE='Next'>";
    print "</FORM>";


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.