Pagination query using SQL - php

I have a table called 'items'
It has the following columns: id, name, stock, price, and category_id.
My query is
SELECT * FROM items WHERE category_id = {$category_id} LIMIT 10;
my logic for next page is store all ids in a variable called $oldIds and the next query is:
SELECT * FROM items WHERE category_id = {$category_id} AND id NOT IN ($oldIds) LIMIT 10
for another page is i store again the ids from 1st page and 2nd page do again the query
SELECT * FROM items WHERE category_id = {$category_id} AND id NOT IN ($oldIds) LIMIT 10
Do i continue using this style of query or is there a better way of querying?

for mysql
First statement should be:
mysql_query("SELECT * FROM {$statement} ORDER BY datetime ASC LIMIT {$limit} OFFSET {offset}
if 10 records per page is what you are looking for and you want to show records on page 2 i.e. records 11-20 your query will look like:
mysql_query("SELECT * FROM {$statement} ORDER BY datetime ASC LIMIT 10 OFFSET 10

Related

Retrieve database entries starting from a particular row

Say I have 50 rows in my database table and I wanted to start retrieving database entries starting with row 9.
How can I retrieve data from a database table starting at row 9?
My code for pulling data from my db table:
$sql = mysql_query("SELECT * FROM listheadlines ORDER BY id DESC LIMIT 20");
You can use OFFSET keyword like this:
$sql = mysql_query("SELECT * FROM listheadlines ORDER BY id DESC LIMIT 20 OFFSET 9");
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter ( mysql.com ).
"SELECT * FROM listheadlines ORDER BY id DESC LIMIT 9, 18446744073709551615"
Or to get only 20 rows starting at offset 9:
"SELECT * FROM listheadlines ORDER BY id DESC LIMIT 9, 20"
Try this
mysql_query("SELECT * FROM listheadlines ORDER BY id Desc").setFirstResult(9);
Please check this once
How to fetch rows from middle of the table in mysql using hibernate?

Pulling specific data on top and restricting its limit in MySQL PHP

I have following sql query which pulls data with the id 627 to top and then it displays data with other ids
$sql_query = "select * from listing ORDER BY case when listing.makaan_id='627' then 1 else 2 end, listing_id DESC LIMIT {$start}, {$limit}";
im using php pagination system to paginate the results where i set the pagination limit to 10. Now i want to show only 4 results on top with the id 627 and rest with other ids, so in a set of 10 results 4 would be from the id 627 and other 6 will be from other ids, do i have any solution for this without writing another separate sql statement?
You can try something like this
SELECT *
FROM listing
WHERE makaan_id = 627
ORDER BY listing_id DESC
LIMIT 4
UNION ALL
SELECT *
FROM listing
WHERE makaan_id <> 627
OR makaan_id IS NULL
ORDER BY listing_id DESC
LIMIT {$start} - 4, {$limit} - 4

mysql - how to count how many records there are in a table and then only get 20?

I'm building a forum for a school project and I want to display only say 20 posts per page, for a forum. However, I still need to know how much total posts there are, to display the page listing.
I could run a mysql query that limits the number of posts to 20, and then, run another query that would count how many records there are in a table, but that would be 2 queries. Isn't there a way to both get a limited number of records AND in the same query, to get the number of records total?
Thanks.
You can use a subquery
select *,
(select count(*) from your_table) as total_count
from your_table
order by some_column
limit 20
I'm expanding juergen's post:
$result = mysql_query("SELECT * (SELECT COUNT(*) FROM table) AS total FROM tablePRDER BY id LIMIT 0, 20");
while($row = mysql_fetch_assoc($result))
{
$total = $row['total']; /* Total rows of the whole table */
$id = $row['id']; /* Id of limitted query (20) */
}
select *
from your_table
inner join (select count(*) as total_count from your_table) tc
order by some_column
limit 20;

PHP - Limit SQL results and show them by id

How can you LIMIT SQL results and show them by order id once you refresh the page? For en example:
Refresh #1;
Show Query with id 20
Rrefresh #2
Show query with id 21
Rrefresh #3
Show query with id 23
etc etc.
You can store the last showed id in a Session and than increment it on the next show.
to limit result you can use LIMIT:
SELECT * FROM `table` LIMIT 5
to order the result use ORDER BY:
SELECT * FROM `table` ORDER BY `id` ASC
Did you mean - how to show latest N items ordered by id in descendant order?
select *
from table
order by id desc
limit 0, 10
(N = 10 in this case)

mysql select statement and limiting the number of records

I am coding a blog post kind of thing, where the author will post the article and it will be displayed in the front end, my problem starts for selecting the posts as i have to meet certain conditions for posting the news in the front end,
I have 4 fields in the database namely
title
pic_title
pic_brief
pic_detail
you guessed it right apart from the title table the rest of three will hold the path to the images in varchar datatype, which will be used to display as the post, the format of the front end is such that
a) there will be total of eight post
displaying in the front end (eight
entries from the database)
b) there will be three post on the top which will include the value from
the table title, pic_title and
pic_brief (total of 3 values)
c) and the rest five will contain just the title and pic_title
(excluding the three entries of top)
Please NOTE: i want the second query to exclude the top 3 record
which already exist in the top i.e
(first query = 3 post in descending
order, second query = 8 - first 3 = 5
post)
The Order of the Post i want is by id DESC
EDIT: I took the first query as
SELECT * FROM news ORDER BY id DESC LIMIT 3
Now if i take the same second query and try populating the values by desc order again the same records will be accessed
In simple words i want a query that will skip the last three records order by id DESC
How do i achieve this feat in PHP?
If you just want the SQL, here it is:
First query
SELECT * FROM `table` LIMIT 3
Second query
SELECT * FROM `table` LIMIT 3,5
(where table is the name of your table of course. Of course you may want to add some ORDER BY clause. To execute these queries in PHP, I suggest reading the manual. If you have any specific problems after doing so, then you can post a new question.
This is a situation where I'd likely opt to select all eight records at once - the less trips to the database, the better.
SELECT t.title,
t.pic_title,
t.pic_brief
FROM TABLE t
ORDER BY t.id DESC
LIMIT 8
...because the rest is just presentation:
$query = sprintf("SELECT t.title,
t.pic_title,
t.pic_brief
FROM TABLE t
ORDER BY t.id DESC
LIMIT 8");
// Perform Query
$result = mysql_query($query) or die( mysql_error() );
$rowcount = 1;
// Use result
while ($row = mysql_fetch_assoc($result)) {
if(rowcount <= 3) {
echo $row['title']
echo $row['pic_title']
echo $row['pic_brief']
} else {
echo $row['title']
echo $row['pic_title']
}
++$rowcount;
}
first query will be like this
"select title, pic_title , pic_brief from table_name order by post_id desc limit 0 , 3"
and rest of five will be
"select title, pic_title from table_name order by post_id desc limit 3 , 5"
second query will exclude the three results returned by first query...
If you want more perfection you can collect all three Ids returned by first query and can add NOT IN in second query.
"select title, pic_title from table_name where post_id not in (1,2,3) order by post_id desc limit 0 , 5";

Categories