1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<?
/*
* addChat, getChat functions
*/
include_once('sqli.php');
define('CHAT_ROWS_TO_KEEP', 50);
function addChat($userID, $message) {
global $mysqli;
if ($message == '')
return;
$stmt = $mysqli->prepare("INSERT INTO `chat`
(`userID`, `message`)
VALUES (?, ?)");
$stmt->bind_param('is', $userID, $message);
$stmt->execute();
$ID = $stmt->insert_id;
$stmt->close();
// $mysqli->close;
return $ID;
}
function getChat($startID = 0) {
global $mysqli;
$array = array();
if (!is_numeric($startID))
return false;
//Not sure why this happens; but just return nothing..
if (!is_object($mysqli))
die("Error creating object");
$res = $mysqli->query("
SELECT
chat.ID, chat.userID, chat.message, chat.dateSent,
users.displayName,
users.displayColor,
users.wallColor,
users.wallEmblem
FROM `chat`
LEFT JOIN `users`
ON chat.userID = users.ID
WHERE chat.ID > '$startID'
ORDER BY chat.ID ASC
");
$array = array();
if ($res->num_rows == 0) {
$res->close();
return false;
}
while ($response = $res->fetch_assoc()) {
$array[] = $response;
$lastID = $response['ID'];
}
$res->close();
if (count($array) < 1)
return false;
//Turnicate messages.
$deleteFromID = $lastID - CHAT_ROWS_TO_KEEP;
$mysqli->query("DELETE FROM `chat` WHERE `ID` < $deleteFromID");
$mysqli->close();
return $array;
}
?>
|