PHP output couple times data from a column - php

How do I output data from column, but to be like.. for example - the column is with 40 lines of query results and I want to output only that, that are from 10 to 30 . How to do it with PHP?

For MySQL, you could use a LIMIT offset, maxrows:
SELECT * FROM tbl LIMIT 9,20;
will get you rows 10-30. Note that the offset value is 0 based, hence the 9 rather than 10.

Related

Selecting the next set of records from a MYSQL database table - PHP

I would like to know the syntax of how to construct a sql query that retrieves sets of data from the database. For example, I am able to retrieve a set of 10 records from a table , but then would like to retrieve the next ten after that based on the same query.
PHP
$limit = 10;
$SQl = "SELECT * FROM myTable LIMIT {$limit}";
What would the sql query be for the next 10 records, given that I already have the previous 10 records and would not want them to also be retrieved?
There are two options:
LIMIT 11, 10
and one more:
LIMIT 10 OFFSET 11
Legend:
11 - start row
10 - how much rows

Print mysql result 10 by 10 php css page break after

For example I have 100 result in my query.
I want to printing paper every page 10 result.
How to print php mysql query result 10 by 10 css page-break-after?
You did not ask your question very well in English.
But I think that you can run your query like this :
SELECT * FROM tableName ORDER BY fieldName DESC LIMIT $startIndex, $count
Which $startIndex could be your starting number and $count could be 10 in your case.
NOTE Do not forget to edit my code and use prepared statement in your query

Why does a Mysql table size affects runtime with LIMIT command

I have 2 tables - info and comments.
info has 270,000 rows, while comments has only 100 rows.
I run a php script that selects the data from the database and encodes it to json format.
The PHP script also limits the response to only first 10 rows, and both PHP files Info.php and Comments.php are exactly the same, except the names.
So why is it, that the table with 270,000 rows takes way more time to load than the one with 100 rows when it only prints the first 10 rows?
comments takes 1 seconds while info takes 10 seconds.
This is the PHP query code, pretty simple :
Info.php: $query = "SELECT * FROM info ORDER BY id LIMIT 10;";
Comments.php: $query = "SELECT * FROM comments ORDER BY id LIMIT 10;";
As for testing purposes, they both have the same columns and same data, the only difference is the rows number. So I tested times with PHP and:
Info.php:
select from database time: 0.6090 seconds
time taken to decode JSON: 6.4736 seconds
while Comments.php results:
select from database time: 0.7309 seconds
time taken to decode JSON: 1.7178 seconds
Thanks
It might be because of MySQL select clause execution order. MySQL engine has to sort all the rows in the table first by id column, and after that limit results to 10 rows. Take a look at this answer.

How can I write an SQL query offset by x rows

I'm trying to figure out the fastest way to get x rows from a table that are offset by x rows and ordered by a date column.
The problem I have is I'm paginating the rows from the query into pages of 10 rows per page, but I only need the nth page.
For example if I only need page 4 from the table, I need to select all the rows:
SELECT * FROM posts ORDER BY date
Then I need to paginate the array using PHP and get the 4th page (if it exists). This is less than ideal as it seems a waste to have to get the whole table.
Is there a better way to query the table in this situation?. For example if I have 10 posts per page and I want the the 4th page, is there a way to offset the query so it starts from the 30th row? (and ordered by date).
You're looking for LIMIT
SELECT * FROM posts ORDER BY date LIMIT 10, 20
Where 10 is offset and 20 is the number of rows
LIMIT is the answer. According to MySQL documentation,
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

mysql limit minimum not being set

well this should be easy for someone---
I am trying to select specific rows in a database
SELECT * FROM customers LIMIT 10,20
but it always returns 20 rows
what is going on, and why does the minimum not have any value?
LIMIT X, Y
means that you get Y results starting from number X+1.
So if you want values from 11 to 20 you need to use:
LIMIT 10, 10
You would want to do limit 10 or lmit 0, 10 for 10 results
The second number of the LIMIT clause is the number of rows to be returned, not the last index. if you only want 10 rows, replace 20 by 10 in your query limit clause.
SELECT *
FROM customers
LIMIT 10, 10
Limit with two arguments (x,y) means you get rows from x+1 to y. From the MySQL Manual:
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10;
Retrieve rows 6-15 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.
This statement
retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
With one argument,
the value specifies the number of rows to return from the beginning of
the result set:
SELECT * FROM tbl LIMIT 5;
Retrieve first 5 rows In other words,
LIMIT row_count is equivalent to LIMIT 0, row_count.

Categories