49 lines
1006 B
PHP
49 lines
1006 B
PHP
<?php
|
|
|
|
createDB();
|
|
|
|
function createDB(): SQLite3 {
|
|
$db = new SQLite3('guestbook.db');
|
|
$db->exec('CREATE TABLE user
|
|
(id_user INTEGER PRIMARY KEY,
|
|
username TEXT NOT NULL UNIQUE,
|
|
password TEXT)'
|
|
);
|
|
$db->exec('CREATE TABLE message
|
|
(id_msg INTEGER PRIMARY KEY,
|
|
id_user INT,
|
|
content TEXT NOT NULL,
|
|
date INT,
|
|
FOREIGN KEY (id_user) REFERENCES user (id_user))'
|
|
);
|
|
return $db;
|
|
}
|
|
|
|
// getMessages(['id' => 34, 'since' => 383489289424])
|
|
|
|
function getMessages(SQLite3 $db, int $limit = 25, array $filter = []) {
|
|
// Apply filters
|
|
$append_string = '';
|
|
$verb = 'WHERE';
|
|
|
|
foreach ($filter as $key => $value) {
|
|
$to_append = match($key) {
|
|
'id_msg' => "$verb $key IN (:$key)",
|
|
// TODO
|
|
};
|
|
if ($to_append) {
|
|
$append_string .= " $to_append";
|
|
$verb = 'AND';
|
|
}
|
|
}
|
|
|
|
$query = 'SELECT * FROM message' . $append_string;
|
|
$statement = $db->prepare($query);
|
|
|
|
foreach ($filter as $key => $value) {
|
|
$statement->bindValue($key, $value);
|
|
}
|
|
|
|
return $statement->execute();
|
|
}
|