Page 1 of 1

Newbie PHP/mySQL Fetch Array Question...

Posted: Mon Jun 02, 2008 3:29 pm
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!

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

Posted: Mon Jun 02, 2008 11:09 pm
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!

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

Posted: Tue Jun 03, 2008 9:21 am
by justink
Cool thanks Ill give this a shot!