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

  • Home
  • SEARCH
  • 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 6021757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:45:35+00:00 2026-05-23T03:45:35+00:00

$sql = SELECT messages.text, users.name FROM messages INNER JOIN users ON messages.user_id=users.id ORDER BY

  • 0
$sql = "SELECT messages.text, users.name FROM messages INNER JOIN users ON messages.user_id=users.id ORDER BY messages.ms_id DESC LIMIT 10";
$result = mysql_query($sql);
$rows = array();
    while($row = mysql_fetch_array($result))
    {
            $rows[]=$row;
    }
    echo $rows[0][1].$rows[0][0];
/*         for($i=0;$i<=10;$i++)
                {

                                  echo $rows[i][1].$rows[i][0];

                }
*/

This script is supposed to show the last 10 messages in a chat.What I’m doing is getting the Name of the user from the users table and the text from the message table and I want to display them in my chat window.Right now I have only 4 messages recorded and don’t know how this affects the whole script I should implement a check for this too, but the bigger problem is that when i use echo $rows[0][1].$rows[0][0]; the info is displayed correctly, but when I try to make a loop so I can show tha last 10 (I tried the commented one) then nothing is displayed.I thought at least when I use this loop I’ll see the 4 recorded messages but what really happen is a blank window.Obvously I have the info recorded in $rows[] and can echo it, but don’t understand why this loop don’t work at all.I’ll appreciate if someone can help me with this and with the check if the messages are less then 10.

Thanks.

Leron
P.S
Here is the edited script, thanks to all of you, I need the array because otherwise the most recent message is shown at the top which is not an opiton when I use it for diplaying chat masseges.

 for($i=10;$i>=0;$i--)
            {
                      if($rows[$i][1]!="" || $rows[$i][0]!="")
                           {
                              echo $rows[$i][1].' : '.$rows[$i][0];
                              echo "</br>";
                           }
            }
  • 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-23T03:45:36+00:00Added an answer on May 23, 2026 at 3:45 am

    Your FOR loop was running 11 times even if only 10 records. The second clause should be a < instead of <=. Plus the $ was missing on the i variable.

    For example sake, you don’t really need to make an array from the rows, and you can refer to the fields by name:

    while($row = mysql_fetch_array($result))
    {
        echo $row['name'] . ' says: ' . $row['message'] . '<BR>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL: SELECT u.id, u.name, isnull(MAX(h.dateCol), '1900-01-01') dateColWithDefault FROM universe u LEFT JOIN history h
The following SQL: SELECT notes + 'SomeText' FROM NotesTable a Give the error: The
The following SQL SELECT * FROM customers converted to this in LINQ var customers
I've got the following SQL: select * from transaction_log where stoptime like '%2008%' How
I am having trouble with a SQL query. SELECT t.topicname, m. *, ms.avatar FROM
I have a simple SQL 'Select' query, and I'd like to dump the results
I have a SQL SELECT query which has a LIKE clause containing an underscore,
I want to create a SQL Select to do a unit test in MS
How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT
What I'm trying to do is run the same SQL select on many Oracle

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.