So I created a chat with jquery php and mysql.. It also uses ajax. So here is the code
The PHP process page:
$result = mysql_query("SELECT *
FROM messages
ORDER BY id DESC
LIMIT 30") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
if($row['correct'] == 1) {
$correct = "<font color='green'>";
} else {
$correct = "<font color='red'>";
}
echo $correct;
echo $row['name']. ": ". $row['message'] . "</font>";
echo "<br />";
}
My jquery on the index page looks like this
$(document).ready(function() {
$('form[name=answer]').submit(function() {
validate();
return false;
});
function validate() {
$.post("http://localhost:8888/school/procees.php",
{answer: $('input[name=answer]').val(),
questionid: $('input[name=questionid]').val(),
name: $('input[name=id]').val()}, 'json');
}
setInterval(function() {
$('#about').load('maketable.php');
}, 1000);
});
I understand that running a check and then printing everything out requires takes a lot of memory I do not know how to do it any other way. Suggestion and criticism of the code is welcome. How can I improve it?
there’s other code you are using here which might/might not help – what I would say is that if this is live chat consider using the MEMORY table type in mysql – it has very fast reads which makes it a good candidate for this type of application.
Make sure your ajax requests only ask for data that you haven’t already loaded – that may help.
If you need more help we’d need to see the maketable.php code too..