The Default code used to LIMIT the results fetched from the MySQL database using PHP is the following,
$sql="SELECT *
FROM `tablename`
WHERE `Type` LIKE '$var1'
LIMIT 0 , 30";
When we need to display results continuously we can alter that code with the following
$start=0; $end=30;
$start=$_GET['start'];
$end=$start+30;
$sql="SELECT *
FROM `tablename`
WHERE `Type` LIKE '$var1'
LIMIT $start , $end";
So I pass the value for the variable start with a link saying Next like this
<a href="something.php?start=<? $start+30 ?>">Next</a>
Everything goes fine when there are more results to display.
Consider this situation: There are 120 Entries in the Database and the PHP file is currently displaying results 91 to 120. So when I click the Next Link now it shows blank. So how can I make the “Next” link to be disabled when it is showing the last set of results? I think that could be possible if we know the total number of entries in the database. But if it is dynamic, how can we calculate it?
If you use a COUNT() function on the result, you can get simple info back.
For example:
The value returned is the total number of entries that fit the given criteria. So:
You can then simply do something along the lines of:
It’s probably also worth mentioning that your SQL conditions are faulty. The way you have it set up, passing a $start value of ’10’ to the file would create the query:
Which grabs 40 results, starting from result 10, not the results between 10 and 40; a common misconception.
While I’m presuming that’s a cut down version of the code, it’s always worth using *mysql_real_escape_string()* on anything from the outside that goes into your query. People could easily put the $start value as “; DROP TABLE
tablename;”, making your query:Which I’m sure you don’t want 😛