Jump to content

hi im new to databases


mattm1712

Recommended Posts

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

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'];
}
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.