Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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). </p> <p>But to your code, lets go through it:</p> <ol> <li>You should ask DB to get count of the rows, not using mysql function, it's far more effective, so use <code>SELECT count(*) FROM mytable</code>.</li> <li>For <code>$pages</code> use ceil() as you want all rows to be printed, if you have <code>$max</code> 5 and have 11 rows, round will make <code>$pages</code> 2, where you actually want 3 (last page just contains that last 11th row)</li> <li>in LIMIT you want to <code>LIMIT row_count OFFSET offset</code>. You can calculate offset from page number, so: <code>$max = row_count</code> but <code>$offset = ($max * $page) - $max</code>. In your code if <code>$get</code> 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.)</li> </ol> <hr> <p>I have prepared simple example here which uses PDO, maybe that gives you idea how simple it's use PDO. </p> <p>The selecting rows shows example how to put parameters in SQL, it would be perfectly safe in this case state, <code>'SELECT * FROM pseudorows LIMIT '.$start.','.$max</code> by I wanted to make an example how easy it is (and then safe):</p> <pre><code>// 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-&gt;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-&gt;prepare('SELECT count(*) FROM pseudorows'); $stmt-&gt;execute(); if($value = $stmt-&gt;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-&gt;prepare('SELECT * FROM pseudorows LIMIT :start,:max'); $stmt-&gt;bindParam(':start',$start,PDO::PARAM_INT); $stmt-&gt;bindParam(':max', $max,PDO::PARAM_INT); $stmt-&gt;execute(); // simply just print rows echo '&lt;table&gt;'; while($row = $stmt-&gt;fetch()) { echo '&lt;tr&gt;&lt;td&gt;#'.$row['id'].'&lt;/td&gt;&lt;td&gt;'.$row['title'].'&lt;/td&gt;&lt;/tr&gt;'; } echo '&lt;/table&gt;'; // let's show pagination for($i=1;$i&lt;=$pages;$i++) { echo '[ &lt;a href="?p='.$i.'"&gt;'.$i.'&lt;/a&gt; ]'; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload