jdock1 Posted July 11, 2010 Share Posted July 11, 2010 I am using a while loop to display the database entries. Now how could I make it display the LAST five entries into the database? Ive been coding all day and I have writers block or something! I cant think. Also, how could I make PHP count how many entries there are in the database? For example, I want to put up on my page how many signatures have been added to the database. How could I do this? I have been searching around everywhere and still couldnt find a solution. Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/ Share on other sites More sharing options...
Pikachu2000 Posted July 11, 2010 Share Posted July 11, 2010 Do you have an auto-incremented index field? Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084609 Share on other sites More sharing options...
Adam Posted July 11, 2010 Share Posted July 11, 2010 Look into the MySQL "LIMIT" clause. E.g. select * from your_table limit 0, 5 That selects only the first 5 rows matched. In order to return the last five, you will need to "ORDER BY" a chronological field; either an auto incrementing number, or a date field. E.g. select * from your_table order by entry_id limit 0, 5 select * from your_table order by entry_date limit 0, 5 Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084615 Share on other sites More sharing options...
jdock1 Posted July 12, 2010 Author Share Posted July 12, 2010 Look into the MySQL "LIMIT" clause. E.g. select * from your_table limit 0, 5 That selects only the first 5 rows matched. In order to return the last five, you will need to "ORDER BY" a chronological field; either an auto incrementing number, or a date field. E.g. select * from your_table order by entry_id limit 0, 5 select * from your_table order by entry_date limit 0, 5 Thanks! Now how could I track how many entries there are in the database and echo it? Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084625 Share on other sites More sharing options...
BillyBoB Posted July 12, 2010 Share Posted July 12, 2010 That would be mysql_num_rows <?php $result = mysql_query("select * from your_table order by entry_date"); //you wouldn't want to limit this query. $rows = mysql_num_rows($result); echo $rows; ?> Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084628 Share on other sites More sharing options...
snizkorod Posted July 12, 2010 Share Posted July 12, 2010 the ORDER BY column DESC LIMIT 0,15 clause can help you. If you have an auto_increment column, you can use that. Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084629 Share on other sites More sharing options...
jdock1 Posted July 12, 2010 Author Share Posted July 12, 2010 That would be mysql_num_rows <?php $result = mysql_query("select * from your_table order by entry_date"); //you wouldn't want to limit this query. $rows = mysql_num_rows($result); echo $rows; ?> Good luck! Thanks, but for some reason I am getting an access denied error (using password NO) even know I established a connection to the database properly..? Its saying the error is on line 4, where the $result variable is being defined. Why would it be giving me that error? My code: <?php $db = mysqli_connect('localhost', 'adsco_ptn', 'password', 'adsco_petitions') or die('Error: Could not connect to database!'); $result = mysql_query("select * from signatures order by first"); //you wouldn't want to limit this query. $rows = mysql_num_rows($result); echo $rows; Error: Warning: mysql_query() [function.mysql-query]: Access denied for user 'adsco'@'localhost' (using password: NO) in /home/adsco/public_html/google/count.php on line 4 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/adsco/public_html/google/count.php on line 4 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/adsco/public_html/google/count.php on line 5 Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084643 Share on other sites More sharing options...
BillyBoB Posted July 12, 2010 Share Posted July 12, 2010 Well that says you didn't put your password in correctly. Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084645 Share on other sites More sharing options...
kenrbnsn Posted July 12, 2010 Share Posted July 12, 2010 You're connecting with mysqli, but you're using mysql for the rest of the code. They are different ways of accesses the database. Pick one or the other. Ken Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084647 Share on other sites More sharing options...
Adam Posted July 12, 2010 Share Posted July 12, 2010 That would be mysql_num_rows <?php $result = mysql_query("select * from your_table order by entry_date"); //you wouldn't want to limit this query. $rows = mysql_num_rows($result); echo $rows; ?> Good luck! "you wouldn't want to limit this query".. why exactly? Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084744 Share on other sites More sharing options...
Pikachu2000 Posted July 12, 2010 Share Posted July 12, 2010 It shouldn't be limited because he asked how to "track how many entries are in the database." Which also brings up that mysql_num_rows() isn't the best way to do that. A SELECT COUNT() query more efficient when simply returning the number of records in a table. Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084824 Share on other sites More sharing options...
Adam Posted July 12, 2010 Share Posted July 12, 2010 In that situation you'd perform a count query first, then another query to return the limited results. Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084857 Share on other sites More sharing options...
Pikachu2000 Posted July 12, 2010 Share Posted July 12, 2010 In that situation you'd perform a count query first, then another query to return the limited results. Zacly. Quote Link to comment https://forums.phpfreaks.com/topic/207454-using-a-while-loop-how-can-i-display-the-last-five-entries-into-the-database/#findComment-1084862 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.