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

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

 

rename connection.inc to php or people will be able to read your database connection info

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.