[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

MySql Select Statement in PHP

Posted on 2001-08-02
3
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
xbite earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

650 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