Compare commits
No commits in common. "e6f541198bdad0bbafe1ae2477ce4812dc61967a" and "63b19062eab30fce1d517697d169075dfe31347c" have entirely different histories.
e6f541198b
...
63b19062ea
|
@ -5,6 +5,6 @@
|
|||
<title>Guestbook</title>
|
||||
</head>
|
||||
<body>
|
||||
Test
|
||||
hola
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,59 +1,15 @@
|
|||
<?php
|
||||
|
||||
$db = createDB();
|
||||
$test = getMessages($db, ['id_user' => 1]);
|
||||
var_dump($test);
|
||||
createDB();
|
||||
|
||||
function createDB($db_name = 'guestbook.db'): SQLite3 {
|
||||
// Can't create table if it already exists
|
||||
$exists = file_exists($db_name);
|
||||
|
||||
$db = new SQLite3($db_name);
|
||||
|
||||
if(!$exists) {
|
||||
$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;
|
||||
function createDB()
|
||||
{
|
||||
$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,
|
||||
FOREIGN KEY (id_user) REFERENCES user (id_user))");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
var_dump($statement);
|
||||
return $statement->execute()->fetchArray(SQLITE3_ASSOC);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue