essjay_d12 Posted March 14, 2006 Share Posted March 14, 2006 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 DATEECHO 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 helpThanks Quote Link to comment https://forums.phpfreaks.com/topic/4955-loops-in-php/ Share on other sites More sharing options...
wildteen88 Posted March 14, 2006 Share Posted March 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4955-loops-in-php/#findComment-17436 Share on other sites More sharing options...
essjay_d12 Posted March 14, 2006 Author Share Posted March 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4955-loops-in-php/#findComment-17442 Share on other sites More sharing options...
wildteen88 Posted March 14, 2006 Share Posted March 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4955-loops-in-php/#findComment-17445 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.