mattm1712 Posted April 5, 2010 Share Posted April 5, 2010 i need a code that posts the latest 10 title in the database i have this but i know its wrong <?php include 'connect.inc'; echo "recent posts"; $recent = mysql_query("SELECT * FROM comments ORDER BY date DESC LIMIT 10"); $array = mysql_fetch_assoc($recent); $post_title = $array['tilte']; while($array = mysql_fetch_assoc($recent)) { echo "$post_tilte"; } ?> Quote Link to comment Share on other sites More sharing options...
graham23s Posted April 5, 2010 Share Posted April 5, 2010 Hi Matt, Did you know echo "$post_tilte"; is spelt wrong? cheers Graham Quote Link to comment Share on other sites More sharing options...
mattm1712 Posted April 5, 2010 Author Share Posted April 5, 2010 ok cheers for that i have that bit working, but it posts the latest post 8 times instead of posting the latest 10? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 5, 2010 Share Posted April 5, 2010 Are there 10 records in the database table, or only 8? Quote Link to comment Share on other sites More sharing options...
mattm1712 Posted April 5, 2010 Author Share Posted April 5, 2010 theres 8 but it repeats the last 1 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 5, 2010 Share Posted April 5, 2010 If there are only 8 records, you can't expect it to retrieve and display 10 of them. But are you saying the loop displays the same record 8 times instead of displaying all 8 of them? The query looks off to me. You are assigning the value to $post_tilte outside of the while{} loop, therefore it never changes. Try this, but note that I didn't correct the spelling of title, so you'll need to edit that if you corrected it locally. <?php include 'connect.inc'; echo "recent posts"; $query = "SELECT tilte FROM comments ORDER BY date DESC LIMIT 10"; // separate the query, and you should only select necessary fields rather than wildcard * all $result = mysql_query($query); //execute the query while($array = mysql_fetch_assoc($result)) { echo $array['tilte']; } ?> Quote Link to comment Share on other sites More sharing options...
Lukeidiot Posted April 5, 2010 Share Posted April 5, 2010 If there are only 8 records, you can't expect it to retrieve and display 10 of them. But are you saying the loop displays the same record 8 times instead of displaying all 8 of them? The query looks off to me. You are assigning the value to $post_tilte outside of the while{} loop, therefore it never changes. Try this, but note that I didn't correct the spelling of title, so you'll need to edit that if you corrected it locally. <?php include 'connect.inc'; echo "recent posts"; $query = "SELECT tilte FROM comments ORDER BY date DESC LIMIT 10"; // separate the query, and you should only select necessary fields rather than wildcard * all $result = mysql_query($query); //execute the query while($array = mysql_fetch_assoc($result)) { echo $array['tilte']; } ?> rename connection.inc to php or people will be able to read your database connection info Quote Link to comment 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.