Solved

Display last row in database

Posted on 2010-09-02
3
386 Views
Last Modified: 2013-12-13
How do I display the last value of the last row in a database?

mysql_select_db($database_conn_elements, $conn_elements);
$query_rs_forum_thread_messages = "SELECT * FROM forum_thread_messages WHERE forum_thread_id = " . $row_rs_forum_thread['id'];
$rs_forum_thread_messages = mysql_query($query_rs_forum_thread_messages, $conn_elements) or die(mysql_error());
$row_rs_forum_thread_messages = mysql_fetch_assoc($rs_forum_thread_messages);
$totalRows_rs_forum_thread_messages = mysql_num_rows($rs_forum_thread_messages);


i.e. when I print the below I only want to see the message column of the last row?

echo $row_rs_forum_thread_messages['message'];

0
Comment
Question by:petewinter
3 Comments
 
LVL 19

Accepted Solution

by:
Bardobrave earned 250 total points
ID: 33586395
Order your query descending by the current field (as you have not defined an order by clause it will be probably ordering by query's first field) and get the first row.
0
 
LVL 2

Assisted Solution

by:Maverickerko
Maverickerko earned 250 total points
ID: 33586417
you need some uniqe identificator as autoincremnted ID in table then you order table by this id descending and then you just return first row 


mysql_select_db($database_conn_elements, $conn_elements);

$query = "SELECT * FROM forum_thread_messages WHERE forum_thread_id = " . $row_rs_forum_thread['id']." ORDER BY id DESC LIMIT 1";
$result = mysql_query($query_rs_forum_thread_messages, $conn_elements) or die(mysql_error());

$last_message = mysql_result($result,0,"message");

$totalRows = mysql_num_rows($result);



echo $last_message;
0
 

Author Closing Comment

by:petewinter
ID: 33586495
Perfect. Many thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now