Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:31:10+00:00 2026-05-26T22:31:10+00:00

Today is my first day ever messing with PHP or MySQL… I tried to

  • 0

Today is my first day ever messing with PHP or MySQL… I tried to make a simple chat program… it works great but it displays the old chats first and the newest chats (which are at the bottom of my table) last (at the bottom of the page).. how can I load the table in reverse order? So it displays the rows front he bottom of the database table at the top of my HTML table?

Here is my code… All in the file called “Chat.php”

<html><head></head><body>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" />
</form>

<?php

$host="****";
$user="****";
$password="****";

$cxn = mysql_pconnect ($host, $user, $password);

mysql_select_db("defaultdb", $cxn);

if (getenv(HTTP_X_FORWARDED_FOR)) {
    $ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
    $ipaddress = getenv(REMOTE_ADDR);
}

$message = $_POST["message"];

mysql_query("INSERT INTO ChatTest (ID, TimeStamp, Message) VALUES ('$ipaddress', NOW(), '$message')");

$data = mysql_query("SELECT * FROM ChatTest") or die(mysql_error()); 
 Print "<table border cellpadding=3>"; 
 Print "<tr>"; 
 Print "<th>ID:</th><th>TimeStamp:</th><th>Message:</th>";
 while($info = mysql_fetch_array( $data )) { 
 Print "<tr>"; 
    Print " <td>".$info['ID'] . "</td> "; 
    Print " <td>".$info['TimeStamp'] . " </td>";
    Print " <td>".$info['Message'] . "</td></tr>"; 
 } 
 Print "</table>"; 

mysql_close($cxn);


?>
</body></html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:31:11+00:00Added an answer on May 26, 2026 at 10:31 pm

    What you are looking for is MySQL’s ORDER BY statement. You can use it to tell the DBMS in which order you want your results.

    SELECT * FROM ChatTest
    ORDER BY `TimeStamp` DESC
    

    And just another thing…

    I realize that this is your first attempt, so it’s understandable that there’ll be mistakes. One thing, however, you should learn about right away is SQL Injections.

    Consider an example where the user’s message is

    0'); DROP TABLE ChatTest --
    

    So suddenly your query would look like

    INSERT INTO ChatTest (ID, TimeStamp, Message)
    VALUES ('$ipaddress', NOW(), '0'); DROP TABLE ChatTest --')
    

    To prevent this, always run user input through mysql_real_escape_string(), like this:

    $message = mysql_real_escape_string($_POST['message']);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is literally my first day using PHP. I've never seen it until today,
Today is the very first day I've ever even seen aspx, so, please bear
I built a very simple chat system... it works great and when the user
Today's my first day with Flex and FlashDevelop. In my ActionScript file, I have
Today is my first day using ASP.NET MVC, and I'm finding it very intriguing.
Please excuse me, today is my first day trying to setup a CI environment
Greetings. I am trying to learn Java and Swing (today is my first day).
Today is officially my first day with C++ :P I've downloaded Visual C++ 2005
I tried using PHPDoc for the first time today and quickly ran into a
Today is the first day I'm looking into Ruby on Rails, and now I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.