Solved

Display last row in database

Posted on 2010-09-02
3
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

626 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