Solved

MySql Select Statement in PHP

Posted on 2001-08-02
3
205 Views
Last Modified: 2008-03-06
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
Comment
Question by:acenad
3 Comments
 

Accepted Solution

by:
xbite earned 50 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:Richard Coffre
Comment Utility
Too late cause of network issues. It dropped just when I want to send my answer.

Pyxos
0
 

Author Comment

by:acenad
Comment Utility
Hello,

Thx xBite!!!

Kind Regards.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now