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"; } ?> Link to comment https://forums.phpfreaks.com/topic/197658-hi-im-new-to-databases/ 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 Link to comment https://forums.phpfreaks.com/topic/197658-hi-im-new-to-databases/#findComment-1037337 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? Link to comment https://forums.phpfreaks.com/topic/197658-hi-im-new-to-databases/#findComment-1037344 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? Link to comment https://forums.phpfreaks.com/topic/197658-hi-im-new-to-databases/#findComment-1037356 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 Link to comment https://forums.phpfreaks.com/topic/197658-hi-im-new-to-databases/#findComment-1037381 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']; } ?> Link to comment https://forums.phpfreaks.com/topic/197658-hi-im-new-to-databases/#findComment-1037385 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 Link to comment https://forums.phpfreaks.com/topic/197658-hi-im-new-to-databases/#findComment-1037387 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.