samoi Posted January 29, 2009 Share Posted January 29, 2009 Hello guys, I need a little help to explain how to limit while loops ! if I want to get an info from the MySQL, using php script, and I need this info to be the last four (for example) desc result! how to get them? an example: <?php // this is only an example I wrote now, please don't focus on other issues, teach me how to limit a while() loop. include("configuration.php"); // let's say I need to see the last four registrants (the most recent) $samoi = "SELECT username FROM users WHERE id < 4 ORDER BY registation_time desc"; // is this correct stament? I mean id < 4 but I need the last 4 registrants ! It dives me crazy ! $samoi_SQL = mysql_query($samoi); while($phpfreaks = mysql_fetch_assoc($samoi_SQL)) { echo $phpfreaks['username']; echo "<br />"; } ?> I really appreciate your help Link to comment https://forums.phpfreaks.com/topic/142910-solved-limit-the-while-results/ Share on other sites More sharing options...
corbin Posted January 29, 2009 Share Posted January 29, 2009 Depending on the data type of registration_time that should be fine. Link to comment https://forums.phpfreaks.com/topic/142910-solved-limit-the-while-results/#findComment-749258 Share on other sites More sharing options...
Philip Posted January 29, 2009 Share Posted January 29, 2009 Try using the LIMIT clause, SELECT `username` FROM `users` ORDER BY `registation_time` desc LIMIT 0,3 It'll grab the 4 most recent signups, depending on what type registation_time (timestamp, etc) Link to comment https://forums.phpfreaks.com/topic/142910-solved-limit-the-while-results/#findComment-749259 Share on other sites More sharing options...
samoi Posted January 29, 2009 Author Share Posted January 29, 2009 Thank you all guys, I havn't tried it, but I just want to learn, because when ever I needed to pull data of the recent entries I wonder how to do it. so doing this by adding LIMIT 0,3 to the statement of the MySQL, is that right? feeling happy ! Link to comment https://forums.phpfreaks.com/topic/142910-solved-limit-the-while-results/#findComment-749260 Share on other sites More sharing options...
Philip Posted January 29, 2009 Share Posted January 29, 2009 Yup Limit is pretty handy Link to comment https://forums.phpfreaks.com/topic/142910-solved-limit-the-while-results/#findComment-749268 Share on other sites More sharing options...
corbin Posted January 29, 2009 Share Posted January 29, 2009 Wow I just had a stupid moment x.x. I didn't even notice the whole wanting 4 thing. Link to comment https://forums.phpfreaks.com/topic/142910-solved-limit-the-while-results/#findComment-749282 Share on other sites More sharing options...
samoi Posted January 29, 2009 Author Share Posted January 29, 2009 Yup Limit is pretty handy Thank you very Much it's very nice help from you all . Link to comment https://forums.phpfreaks.com/topic/142910-solved-limit-the-while-results/#findComment-749283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.