• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

MySql Select Statement in PHP

Hello,

In ASP when I do a select statement I could specify the number of rows I want, e.g. "Select Top 5 from aTable". Then I executed the query.

But in MySql this statement is not valid (TOP 5). The statement below (in PHP):

$result = mysql_query ("SELECT Top 5 FROM $table") or die ("Invalid query");

generates the error message "invalid query".

My Question:
How can I select some rows only (say 5) instead of the whole recordset???

Thanks,
nad

0
acenad
Asked:
acenad
1 Solution
 
xbiteCommented:
if you want to select just a few rows, you can do it like this:

this one selects first 10 rows
SELECT * FROM tbl_name LIMIT 10

if you want to select last 10 rows:
SELECT * FROM tbl_name ORDER BY some_col DESC LIMIT 10

if you want to select rows 20, 21, 22, 23 ... 30:
SELECT * FROM tbl_name LIMIT 20, 10

you must keep in mind that LIMIT option comes last, so every ORDERing and WHERE part of SELECT will come first, and than, at the end LIMIT

LIMIT has two (2) options,

if you just use one than you specify how many rows will be selected starting from row 1...

if you use both, firstone is used for start point of limit and second one for range of limit.

0
 
Richard CoffreCommented:
Too late cause of network issues. It dropped just when I want to send my answer.

Pyxos
0
 
acenadAuthor Commented:
Hello,

Thx xBite!!!

Kind Regards.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now