Solved

MySql Select Statement in PHP

Posted on 2001-08-02
3
214 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
ID: 6343961
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
ID: 6343996
Too late cause of network issues. It dropped just when I want to send my answer.

Pyxos
0
 

Author Comment

by:acenad
ID: 6344159
Hello,

Thx xBite!!!

Kind Regards.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

816 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

14 Experts available now in Live!

Get 1:1 Help Now