Jump to content

Archived

This topic is now archived and is closed to further replies.

essjay_d12

Loops in php

Recommended Posts

I want to pull out the latest 10 results from my database, but im just not sure, i was thinking like this...

loop (1 to 10) {

query = Select NAME,.,..,.,..,DATE FROm database ORDER BY DATE

ECHO RESULTS

}

But I think the above structure (obviousy not code) is still wrong, wont that above structure when implemented in php just pull out 10 of the same ones (i.e the latest one only) and not the latest 10!

PLease help

Thanks

Share this post


Link to post
Share on other sites
Just do your normall mysql query like so:
[code]<?php

//database connection code goes here

$sql = "SELECT name, email, date From users ORDER BY date";
$result = mysql_query($sql) or die("Error with query " . mysql_query());

while($row = mysql_fetch_array($result))
{
    echo $row['name'] . '<br>';
    echo $row['email'] . '<br>';
    echo $row['date'] . '<br>';
}

?>[/code]Ands that is all there is to getting data out of mysql and displaying it.

Share this post


Link to post
Share on other sites
Yes but there are 20 rows in the table all together and I only want 10, and i want all 20 to be ordered by date so the 10 i pull out are the latest ones.

Share this post


Link to post
Share on other sites
If you just want the first ten rows ordered by date then append your query with LIMIT 1, 10 like so:
[code]$sql = "SELECT name, email, date From users ORDER BY date LIMIT 1, 10 ";[/code]
MySQL will query the database and order its results by the date and then it'll limit the already ordered rows by 10. So only 10 or less results (depending on how many rows there are in the database) should be returned.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.