We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,842 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to Display Latest Entry In MysQL Table

Hi

I am attempting to build my own cms (although very basic) using php and mysql. Instead of having a static homepage, I want the content displayed to be dynamic. In other words, I want the homepage to be flexible/changeable based on whatever the latest 'content' entry into the database is.

I currently have two tables: 'user', and 'homepage'.

My homepage table is structured as follows:

home_id (int)
user_id (index, fk)
title_cont (varchar)
home_cont  (varchar)
home_date (timestamp)

I want to be able to display the latest 'title_cont', and the 'latest home_cont' entry. I am assuming that the date of when an entry is made is going to be useful in order to achieve this, so have included it in the table using a timestamp type.

Here is what I think a solution may be:

<?
$sql = "SELECT * FROM home ORDER BY home_date DESC LIMIT 1";
$display = $db->Execute( $sql );
?>

Then to display on my homepage:

<h2><?=$display->fields['title_cont']?></h2>
<p><?=$display->fields['home_cont']?>

Is this correct? Or is there a better way? Also, should I be using timestamp or datetime?

Thanks

5
Contributors
4
Replies
2 Weeks
Discussion Span
6 Months Ago
Last Updated
5
Views
atkinsonsits
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Is this correct?

Yes.

The useful part of datetime over a timestamp is that you can use the MySQL date functions.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Thanks - would you recommend using datetime over timestamp in this case then? I only need it to sort the latest entry, so that php knows which entry to display.

Does the code look ok? I was going to use:

<?php
while ($row = mysql_fetch_array($sql)) {
echo "<h1>".$row{'title_cont'}."</h1>" "<p>".$row{'home_cont'}."</p>";
}
?>

But then I cam across the other approach - which looks a little unusual to me i.e

<?=$display->fields['value']?>

...I'm not familiar with using "<?=" instead of "echo" or "print". Is it an OO approach?
Try as I have done to pick up OO PHP, I have found it impossible to learn on my own (without having £££ to spend on Zend Training etc) so I have reverted to traditional methods...

Thanks

atkinsonsits
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The benefit of using DateTime is that you can perform actions on the data in the query, MySQL has a host of functions that will work on DateTime fields.
If you really need the date as a Unix timestamp, then there is a MySQL function to convert DateTime to a timestamp, and also PHP has a function that will do the same.

As for <?=, it is simply shorthand for <?php echo.... It should not be used on projects that will, or could, be deployed on unknown configurations, as it requires the PHP short tags option to be enabled.

Will Gresham
Master Poster
772 posts since May 2008
Reputation Points: 95
Solved Threads: 129
Skill Endorsements: 1

Use the select top 1 query statement. Here! and order it by ID, set to asc if you want the first row and desc if last row.

Icone
Junior Poster in Training
87 posts since Oct 2012
Reputation Points: -1
Solved Threads: 5
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0679 seconds using 2.73MB