arpowers Posted October 19, 2007 Share Posted October 19, 2007 Hey! here is my problem.... I am writing a script that needs to pull all users from a relational table. I would like to return the total number of users affiliated in the table but I would also like to limit the array fetched to a suitable number to be displayed on the screen (say.. 6).. so if I use Code: $total_num = mysql_num_rows($result); while($row = mysql_fetch_array($result)){ //this will get data for every user... i don't want to do that. $data[] = $row; } so what I want from this one query is enough to get this info.. I want to: 1. get a total number of users affiliated (e.g. you have 22 users affiliated) 2. only output a certain lower number for output to page. (e.g. user1 - user2 - user3- user4 etc...) Thanks!~ Suggestions? Back to top View user's profile Send private message Quote Link to comment https://forums.phpfreaks.com/topic/73911-mysql_fetch-_array-help-total-number-rows-vs-output/ Share on other sites More sharing options...
enoyhs Posted October 19, 2007 Share Posted October 19, 2007 Try "for" cycle. for ($i=1; $i<=$n; $i++) { $data[] = mysql_fetch_array($result); } Just change "n" to whatever number you want... Quote Link to comment https://forums.phpfreaks.com/topic/73911-mysql_fetch-_array-help-total-number-rows-vs-output/#findComment-372962 Share on other sites More sharing options...
steve448 Posted October 19, 2007 Share Posted October 19, 2007 You could also change your sql query to limit the number of rows returned and use SQL_CALC_FOUND_ROWS to get the total that would have been returned. Quote Link to comment https://forums.phpfreaks.com/topic/73911-mysql_fetch-_array-help-total-number-rows-vs-output/#findComment-373058 Share on other sites More sharing options...
New Coder Posted October 19, 2007 Share Posted October 19, 2007 $sql ="select top 6 column1, column2 from mytable"; Quote Link to comment https://forums.phpfreaks.com/topic/73911-mysql_fetch-_array-help-total-number-rows-vs-output/#findComment-373060 Share on other sites More sharing options...
hostfreak Posted October 19, 2007 Share Posted October 19, 2007 I would use mysql's LIMIT: $sql = "SELECT ... FROM ... LIMIT 0, 6"; Quote Link to comment https://forums.phpfreaks.com/topic/73911-mysql_fetch-_array-help-total-number-rows-vs-output/#findComment-373064 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.