moved things
This commit is contained in:
25
tpCrudTwig/src/DataBase.php
Normal file
25
tpCrudTwig/src/DataBase.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
function ConnectDataBase()
|
||||
{
|
||||
global $db;
|
||||
try {
|
||||
|
||||
$host = "localhost";
|
||||
$user = "root";
|
||||
$password = "motdepasse";
|
||||
|
||||
// Connexion à la bdd
|
||||
$db = new PDO("mysql:host=$host;dbname=cruddb", $user, $password);
|
||||
$db->exec('SET NAMES "UTF8"');
|
||||
} catch (PDOException $e) {
|
||||
echo 'Erreur : ' . $e->getMessage();
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
function CloseDataBase()
|
||||
{
|
||||
global $db;
|
||||
$db = null;
|
||||
}
|
||||
0
tpCrudTwig/src/Role.php
Normal file
0
tpCrudTwig/src/Role.php
Normal file
88
tpCrudTwig/src/User.php
Normal file
88
tpCrudTwig/src/User.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
require_once('DataBase.php');
|
||||
|
||||
function GetUsers()
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = 'SELECT * FROM `users`';
|
||||
$query = $db->prepare($sql);
|
||||
$query->execute();
|
||||
$result = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
CloseDataBase();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function GetUser(int $id)
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = "SELECT * FROM `users` WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
$result = $query->fetch();
|
||||
|
||||
CloseDataBase();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function AddUser(string $login, string $password, string $lastname, int $role, string $firstname, string $description)
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = "INSERT INTO `users` (`login`, `password`, `firstname`, `lastname`, `description`, `role`, `enabled`) VALUES (:login, :password, :firstname, :lastname, :description, :role, :enabled);";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':login', $login, PDO::PARAM_STR);
|
||||
$query->bindValue(':password', $password, PDO::PARAM_STR);
|
||||
$query->bindValue(':firstname', $firstname, PDO::PARAM_STR);
|
||||
$query->bindValue(':lastname', $lastname, PDO::PARAM_STR);
|
||||
$query->bindValue(':description', $description, PDO::PARAM_STR);
|
||||
$query->bindValue(':role', $role, PDO::PARAM_INT);
|
||||
$query->bindValue(':enabled', 1, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
|
||||
CloseDataBase();
|
||||
}
|
||||
|
||||
function UpdateUser(int $id, string $login, string $description, string $role)
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = "UPDATE `users` SET `login`=:login, `description`=:description,
|
||||
`role`=:role WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':login', $login, PDO::PARAM_STR);
|
||||
$query->bindValue(':description', $description, PDO::PARAM_STR);
|
||||
$query->bindValue(':role', $role, PDO::PARAM_INT);
|
||||
$query->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
|
||||
CloseDataBase();
|
||||
}
|
||||
|
||||
function DeleteUser(int $id)
|
||||
{
|
||||
global $db;
|
||||
|
||||
ConnectDataBase();
|
||||
|
||||
$sql = "DELETE FROM `users` WHERE `id`=:id;";
|
||||
$query = $db->prepare($sql);
|
||||
$query->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$query->execute();
|
||||
|
||||
CloseDataBase();
|
||||
}
|
||||
Reference in New Issue
Block a user