Solved

MySql Select Statement in PHP

Posted on 2001-08-02
3
219 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

691 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