Newbie PHP/mySQL Fetch Array Question...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
justink
Forum Newbie
Posts: 2
Joined: Mon Jun 02, 2008 3:21 pm

Newbie PHP/mySQL Fetch Array Question...

Post by justink »

Hi everyone. Im a novice PHP/mySQL guy and Im trying to work something out for my website. Essentially I have been using a flat file text database to update listings for performances at my job's venue. It is getting tedious to update the text file all the time so I created a mySQL database and installed an awesome table editor that allows me to add and delete quickly. But Im having trouble figuring out how to display the information coming from the database.

Before it was pretty simple. I used a while loop and extracted the performance information in loop for a total of 6 listings. Ive been messing around with mysql_fetch_array and cant see to figure out how to limit my results to 6 rows only. Everything I come across is:

Code: Select all

while ($row = mysql_fetch_array($result))
which just returns everything. Could anyone help me figure out to to limit my rows to only 6? Thanks!
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Newbie PHP/mySQL Fetch Array Question...

Post by nowaydown1 »

Hey Justin. When you're working with SQL, any limiting becomes part of your query. Take the following example:

Code: Select all

 
$myDemoQuery = "SELECT * FROM myinfotable LIMIT 5";
$result = mysql_query($myDemoQuery) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
        echo $row["somecolumn"];
}
 
You can see how I shoved the LIMIT clause in there. You can read more about working with LIMIT's here:
http://dev.mysql.com/doc/refman/5.0/en/select.html

About a third of the way down that page there's a bit that reads:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).

That's the part you're interested in. Hope that helps!
justink
Forum Newbie
Posts: 2
Joined: Mon Jun 02, 2008 3:21 pm

Re: Newbie PHP/mySQL Fetch Array Question...

Post by justink »

Cool thanks Ill give this a shot!
Post Reply