Forum Moderators: open
else {
$last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0;
$chatqry=mysql_query("SELECT * from message");
$chatqry=mysql_num_rows($chatqry);
$chatlim=($chatqry-10);
$sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time" .
" FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > " . $last . " LIMIT $chatlim, 10";
$message_query = db_query($sql);
//Loop through each message and create an XML message node for each.
while($message_array = db_fetch_array($message_query)) {
$xml .= '<message id="' . $message_array['message_id'] . '">';
$xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>';
$xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>';
$xml .= '<time>' . $message_array['post_time'] . '</time>';
$xml .= '</message>';
}
}
********************** The above code gets the number of messages in the database, and then sets the starting point 10 from the last message, thus only returning the last 10 messages in the query. This does return the proper query, but the chat fails to update, unless the user manually updates the entire page.
I am not all that familiar with ajax or the xml counterparts being used in this chat. Any assistance in this matter would be greatly appreciated
I took a quick look. The application is sending an AJAX request every 2 seconds to get the latest chat messages, passing the message_id value of the 'last' message it received (the SELECT query uses this value to get only the records with a higher message_id). The first time a user hits the page, that value will be 0 (in other words, it will get all records).
First, the number of records that you are counting is not going to be the same as the actual number returned by the subsequent query. You are doing this:
SELECT * from message
SELECT message_id
FROM message
WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > " . $last
Lets say my table has 100 messages in it. The first time the page is hit, $last = 0, so this will result in a query like:
SELECT message_id FROM message WHERE chat_id = 1 AND message_id > 0
LIMIT 90,10
SELECT message_id FROM message WHERE chat_id = 1 AND message_id > 100
LIMIT -9, 10
<font color="#000000"><b>1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-9,10' at line 1<br><br>SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time FROM message WHERE chat_id = 1 AND message_id > 100 LIMIT -9,10<br><br><small><font color="#ff0000">[STOP]</font></small><br><br></b></font>
Once 10 new records have arrived, you would then see those new messages because you'd have a limit like this:
LIMIT 0, 10
So, you need to do 2 things.
1. You need to make sure LIMIT will never contain a negative x value:
2. You need to differentiate between a first time load and the differential updates. For the differential updates, you don't want to limit the number of rows fetched, you want everything newer than the last message.
Here are the changes that I made to get this working:
[/pre][pre]
} else {
$last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0;
$limit = ''; // No limit unless this is a first time load
if ($last == 0 ¦¦ $last == '0') { // First time load, so limit the results
$chatqry = mysql_query("SELECT message_id FROM message WHERE chat_id = " .
db_input($_GET['chat']) . " AND message_id > " . $last);
$chatqry = mysql_num_rows($chatqry);
$limitDuration = 10; // The max number of rows to fetch on first load
if ($chatqry > $limitDuration) {
$limit = "LIMIT " . ($chatqry - $limitDuration) . ', ' . $limitDuration;
}
}
$sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time" .
" FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > " . $last .
' ' . $limit;
$message_query = db_query($sql);
Give that a try and let us know if it works.