You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
58 lines
1.5 KiB
<?php
|
|
|
|
namespace App;
|
|
|
|
use PDO;
|
|
|
|
class Connexion {
|
|
private PDO $pdo;
|
|
|
|
/**
|
|
* @param PDO $pdo
|
|
*/
|
|
public function __construct(PDO $pdo) {
|
|
$this->pdo = $pdo;
|
|
}
|
|
|
|
public function lastInsertId(): string {
|
|
return $this->pdo->lastInsertId();
|
|
}
|
|
|
|
/**
|
|
* execute a request
|
|
* @param string $query
|
|
* @param array<string, array<mixed, int>> $args
|
|
* @return void
|
|
*/
|
|
public function exec(string $query, array $args) {
|
|
$stmnt = $this->prepare($query, $args);
|
|
$stmnt->execute();
|
|
}
|
|
|
|
/**
|
|
* Execute a request, and return the returned rows
|
|
* @param string $query the SQL request
|
|
* @param array<string, array<mixed, int>> $args an array containing the arguments label, value and type: ex: `[":label" => [$value, PDO::PARAM_TYPE]`
|
|
* @return array<string, mixed>[] the returned rows of the request
|
|
*/
|
|
public function fetch(string $query, array $args): array {
|
|
$stmnt = $this->prepare($query, $args);
|
|
$stmnt->execute();
|
|
return $stmnt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
/**
|
|
* @param string $query
|
|
* @param array<string, array<mixed, int>> $args
|
|
* @return \PDOStatement
|
|
*/
|
|
private function prepare(string $query, array $args): \PDOStatement {
|
|
$stmnt = $this->pdo->prepare($query);
|
|
foreach ($args as $name => $value) {
|
|
$stmnt->bindValue($name, $value[0], $value[1]);
|
|
}
|
|
return $stmnt;
|
|
}
|
|
|
|
}
|