So I am working on a messanger, and I am having problems automatically fetching new messages from the database.
If anyone can fix me up with a peice of code which can fetch new information from the database after every 2 seconds or so.
I am very new to working with PHP and databases, but this is the code I got so far.
<?php $loadRecent = $db->fetchAll("SELECT * FROM messages WHERE fromUser = ? AND toUser = ? OR fromUser = ? AND toUser = ? ORDER BY dateTime, id DESC", array($ownerID, $myID, $myID, $ownerID)); ?>
<?php
if($loadRecent) {
foreach($loadRecent as $recent) {
$messageID = $recent['id'];
$message = substr($recent['message'], 0, 200);
$dateTime = $recent['dateTime'];
$fromUser = $recent['fromUser'];
$fromName = $recent['fromName'];
$readUnread2 = $recent['readUnread'];
if($readUnread2 == 0) {
$readUnread = '(Unread)';
} else {
$readUnread = '';
}
if($fromUser == $myID) {
echo "
<div class=\"msg\">$message</div>
<div class=\"msgsentby\">{$fromName} @ {$dateTime}</div>
";
} else {
echo "<div class=\"msg msgfrom\">$message</div>
<div class=\"msgsentby msgsentbyfrom\">{$fromName} @ {$dateTime}</div>";
}
}
} else {
echo "<center>Your recently messages will appear here!</center>";
}
?>