Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:51:40+00:00 2026-06-04T22:51:40+00:00

I am creating a pagination script and I need to get the first and

  • 0

I am creating a pagination script and I need to get the first and last results in the database query so that I can determine what results appear when the user clicks a page to go to. This is the code that I have at the minute:

// my database connection is opened

// this gets all of the entries in the database
$q = mysql_query("SELECT * FROM my_table ORDER BY id ASC");
$count = mysql_num_rows($q);
// this is how many results I want to display
$max = 2;
// this determines how many pages there will be
$pages = round($count/$max,0);
// this is where I think my script goes wrong
// I want to get the last result of the first page
// or the first result of the previous page
// so the query can start where the last query left off
// I've tried a few different things to get this script to work
// but I think that I need to get the first or last result of the previous page
// but I don't know how to.
$get = $_GET['p'];
$pn = $_GET['pn'];
$pq = mysql_query("SELECT * FROM my_table ORDER BY id ASC LIMIT $max OFFSET $get");

// my query results appear

if(!$pn) {
    $pn = 1;
}
echo "</table><br />
Page $pn of $pages<br />";
for($p = 1;$p<=$pages;$p++) {
    echo "<a href='javascript:void(0);' onclick='nextPage($max, $p);' title='Page $p'>Page $p</a> ";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:51:43+00:00Added an answer on June 4, 2026 at 10:51 pm

    I think you have few problems there, but I try to tackle them for you. First, as comments say above, you are using code that it vulnerable to SQL injection. Take care of that – you might want to use PDO, which is as easy use as MySQL extension, and will save you from many trouble (like injection).

    But to your code, lets go through it:

    1. You should ask DB to get count of the rows, not using mysql function, it’s far more effective, so use SELECT count(*) FROM mytable.
    2. For $pages use ceil() as you want all rows to be printed, if you have $max 5 and have 11 rows, round will make $pages 2, where you actually want 3 (last page just contains that last 11th row)
    3. in LIMIT you want to LIMIT row_count OFFSET offset. You can calculate offset from page number, so: $max = row_count but $offset = ($max * $page) - $max. In your code if $get is directly the page, it means you get $get’th row (Not sure though what happens in your JS nextpage. Bare in mind that not all use JavaScript.)

    I have prepared simple example here which uses PDO, maybe that gives you idea how simple it’s use PDO.

    The selecting rows shows example how to put parameters in SQL, it would be perfectly safe in this case state, 'SELECT * FROM pseudorows LIMIT '.$start.','.$max by I wanted to make an example how easy it is (and then safe):

    // DB config
    $DB_NAME    = 'test';
    $DB_USER    = 'test';
    $DB_PASSWD  = 'test';
    
        // make connection
    try {
        $DB_CONN = new PDO("mysql:host=localhost;dbname=".$DB_NAME, $DB_USER, $DB_PASSWD);
        $DB_CONN->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        die($e);
    }
    
    // lets say user param 'p' is page, we cast it int, just to be safe
    $page   = (int) (isset($_GET['p'])?$_GET['p']:1);
    // max rows in page
    $max    = 20;
    
    // first select count of all rows in the table
    $stmt = $DB_CONN->prepare('SELECT count(*) FROM pseudorows');
    $stmt->execute();
    if($value = $stmt->fetch()) {
        // now we know how many pages we must print in pagination
        // it's $value/$max = pages
        $pages = ceil($value[0]/$max);
    
        // now let's print this page results, we are on $page page
        // we start from position max_rows_in_page * page_we_are_in - max_rows_in_page
        // (as first page is 1 not 0, and rows in DB start from 0 when LIMITing)
        $start = ($page * $max) - $max;
        $stmt = $DB_CONN->prepare('SELECT * FROM pseudorows LIMIT :start,:max');
        $stmt->bindParam(':start',$start,PDO::PARAM_INT);
        $stmt->bindParam(':max',  $max,PDO::PARAM_INT);
        $stmt->execute();
    
        // simply just print rows
        echo '<table>';
        while($row = $stmt->fetch()) {
            echo '<tr><td>#'.$row['id'].'</td><td>'.$row['title'].'</td></tr>';
        }
        echo '</table>';
    
        // let's show pagination
        for($i=1;$i<=$pages;$i++) {
            echo '[ <a href="?p='.$i.'">'.$i.'</a> ]';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a simple RPG game, first time using XNA. Trying to get my character
I'm having some problems with creating pagination with a HABTM relationship. First, the tables
I'm creating a jquery plugin for sorting and pagination a table (I know that
I am basically creating an iphone app that get's it's data from wordpress. Wordpress
I've been thinking of creating my own custom query to get data using $wpdb
Hey, i am currently creating a pagination script using php and jquery and for
I am creating a web page where user can select datasource and execute select
I am creating MVC3 Site and using pagination (using PagedList;) in that to display
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a google map with store locations within 50 miles of user entered address.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.