Solved

Display last row in database

Posted on 2010-09-02
3
403 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…

856 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