Jump to content

X--SOLVED--Xnoob here needs help


ludjer

Recommended Posts

here is my code
[code]
<?php
require_once('../config/database.php');
require_once('../config/newschecksesion.php');

$result = mysql_query("SELECT * FROM news");

while ( $row = mysql_fetch_array($result) )
{?>

<div style="width:450px;">

<h3><u>title:<br />
<?php echo @$row['title']; ?></u></h3>
<p>content:<br />
<?php echo @$row['content']; ?></p>
<p><b>Posted On:</b><i><?php echo @$row['date']; ?> by <?php echo @$row['user']; ?></i></p>
<hr />

</div>


<?php }?> [/code]

now how do i only read the latest rows that where put into the database

thx
ludger
Link to comment
Share on other sites

$result = mysql_query("SELECT * FROM news");

needs to be

$result = mysql_query("SELECT TOP 5 * FROM news ORDER by date DESC");
or
$result = mysql_query("SELECT * FROM news ORDER by date  DESC LIMIT 5");


Just an example of how it can be done

EDIT: replace 'date' for the fieldname in your database where you store the date/datetime stamp

Regards
Liam
Link to comment
Share on other sites

What do you mean by 'latest rows', do you mean the last row or the last couple of rows. If its the last row and you have an auto incrementing id field you can use mysql_insert_id to grab the latest inserted record. Or if you want the last couple of rows use LIMIT and ORDER BY

[code]SELECT * FROM news ORDER by id_col DESC LIMIT 3[/code]
That'll grab the last 3 rows in the news table.

Umm too slow shocker beat me :( ;)
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.