Help - Search - Members - Calendar
Full Version: [resolved] Displaying Latest Entry
Codegrrl.com Forums > Script Help > Tutorial Help
Steven
I'd like to add a smattering of the latest blog entry on my homepage. (Along with a link to the single entry). How would I choose the latest entry form my database? I have to choose the largest id, but how do I do this? I'm new to database manipulation with PHP.

Thanks,
Steven
Amanda
Your SQL query would be something like this:

SELECT * FROM `table` ORDER BY timestamp DESC LIMIT 1

I like ordering by "timestamps" since I might backdate an entry or something... smile.gif
Steven
How would I print that? I'm new to MySQL requests with PHP. smile.gif
Amanda
Oh, OK. Well....hope this is what you want to know. This snippet will print the title of the newest entry linked to the individual entry page.

Start your page and connect to your database
CODE
<?php

//login
mysql_connect("localhost", "username", "password");
//database with tables
mysql_select_db("database");


And make a query, fetch results......
CODE
//put in parts for easy understanding

//the SQL statement
$sql = "SELECT * FROM blog_table ORDER BY timestamp DESC LIMIT 1";
//query
$query = mysql_query("$sql");
//get results
while($row = mysql_fetch_array($query)) {
//do one of these for each row, id, title, whatever you want...

$id = $row["id"];
$title = stripslashes($row['title']);

//...  more here


Format what prints on the page
CODE
//display a sample link to the newest entry with the title and remember those \ before " !!
print("<a href=\"entry.php?id=$id\">$title</a>");

//close loop
}

?>


.. I broke up the code so people can try and learn something instead of copy and paste wink.gif
Steven
Thanks, it works! I also understand how requests work a little better!

It now displays the whole message, which I plan to shorten.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.