Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

Display last row in database

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
petewinter
Asked:
petewinter
2 Solutions
 
BardobraveCommented:
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
 
MaverickerkoCommented:
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
 
petewinterAuthor Commented:
Perfect. Many thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now