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; } function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) { // Apply filters $append_string = ''; $verb = 'WHERE'; foreach($filter as $key => $value) { $to_append = match($key) { 'id_user' => "$verb $key = :$key", // TODO }; if($to_append) { $append_string .= " $to_append"; $verb = 'AND'; } } // Prepare statement $query = 'SELECT * FROM message' . $append_string; //var_dump($query); $statement = $db->prepare($query); foreach($filter as $key => $value) { $statement->bindParam($key, $value); } $result = $statement->execute(); $notnull = true; $ret = []; while($notnull) { $arr = $result->fetchArray(SQLITE3_ASSOC); //var_dump($arr); if($arr !== false) { $ret[] = $arr; } else $notnull = false; } return $ret; } function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) { $query = 'INSERT INTO message(id_user, content, date) VALUES (:id_user, :content, :date)'; $date = (new DateTime('now'))->getTimestamp(); $statement = $db->prepare($query); $statement->bindParam(':id_user', $user_id); $statement->bindParam(':content', $content); $statement->bindParam(':date', $date); $statement->execute(); } function deleteMessage(SQLite3 $db, int $message_id) : int { $query = 'DELETE FROM message WHERE id_msg = :id_msg'; $statement = $db->prepare($query); $statement->bindParam(':id_msg', $message_id); $result = $statement->execute(); if ($result === false) { return -1; } // Number of changed rows return $db->changes(); }