DirektiveDesDons/BancaDati/BancaDati.php

136 lines
3.8 KiB
PHP
Raw Normal View History

2022-12-21 10:14:44 +01:00
<?php
namespace BancaDati;
2023-01-11 11:02:42 +01:00
use PDO;
use PDOException;
2022-12-21 10:14:44 +01:00
class BancaDati {
private $dbName = "BancaDati";
private $linkName = "localhost";
private $user = "root";
private $pw = "root";
public $pdo;
public function __construct() {
$this->linkDB();
}
private function linkDB() {
try {
2023-01-11 11:02:42 +01:00
$this->pdo = new PDO("mysql:dbname=$this->dbName;host=$this->linkName"
2022-12-21 10:14:44 +01:00
, $this->user
, $this->pw
2023-01-11 11:02:42 +01:00
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
2023-01-19 18:37:48 +01:00
var_dump($e);
2022-12-21 10:14:44 +01:00
die;
}
}
public function createUUID()
{
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
2022-12-21 11:19:40 +01:00
/**
* Einheitliche Insert Funktion
* @param string $table
* @param array $values
* @return void
* author Simon Bock
*/
public function insert(string $table, array $values){
$value = "";
$column = "";
foreach ($values as $col => $v){
2023-01-18 09:09:39 +01:00
$value .= "'" . $v . "',";
2022-12-21 11:19:40 +01:00
$column .= $col . ",";
}
$value = trim($value, ",");
$column = trim($column, ",");
$sql = "INSERT INTO $table($column) VALUES ($value);";
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
2023-01-11 11:02:42 +01:00
}catch (PDOException $e){
2022-12-21 11:19:40 +01:00
die;
}
}
/**
* Einheitliche Update Funktion
* @param string $table
* @param string $id
* @param array $values
* @return void
* @author Malte Schulze Hobeling
*/
2022-12-21 10:57:49 +01:00
public function update(string $table, string $id, array $values){
$value = "";
foreach ($values as $col => $v){
2023-01-18 09:09:39 +01:00
$value .= $col . "='" . $v . "',";
2022-12-21 10:57:49 +01:00
}
$value = trim($value, ",");
2022-12-21 10:39:28 +01:00
$sql = "UPDATE " . $table . " SET " . $value . " WHERE `id` = " . $id . ";";
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
2023-01-11 11:02:42 +01:00
}catch (PDOException $e){
2022-12-21 10:39:28 +01:00
die;
}
}
2022-12-21 11:19:40 +01:00
/**
* Einheitliche Delete Funktion
* @param string $table
* @param string $id
* @return void
* @author Malte Schulze Hobeling
*/
public function delete(string $table, string $id){
2023-01-18 09:09:39 +01:00
$sql = "DELETE FROM " . $table . " WHERE `id` = '" . $id . "';";
2022-12-21 11:19:40 +01:00
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
2023-01-11 11:02:42 +01:00
}catch (PDOException $e){
2022-12-21 11:19:40 +01:00
die;
}
}
2023-01-11 11:02:42 +01:00
2023-01-12 14:05:19 +01:00
/**
* einheitliche Select Funktion
2023-01-18 10:51:28 +01:00
* kann sortiert werden durch ["by"]=>"col" und ["order"]=>"ASC|DESC"
2023-01-12 14:05:19 +01:00
* @param string $table
* @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden
* @return void
* @author Malte Schulze Hobeling
*/
2023-01-19 18:37:48 +01:00
public function select(string $table, array $where = []){
2023-01-12 14:05:19 +01:00
$whereString = "";
2023-01-18 10:51:28 +01:00
$orderString = "";
if(isset($where["by"])){
$orderString = " ORDER BY " . $where["by"];
unset($where["by"]);
if(isset($where["order"])){
$orderString .= " " . $where["order"];
unset($where["order"]);
}
}
2023-01-12 14:05:19 +01:00
foreach ($where as $col => $v) {
if($whereString != ""){
$whereString .= " AND ";
2023-01-11 14:40:12 +01:00
}
2023-01-18 09:13:01 +01:00
$whereString .= "`" . $col . "` LIKE '" . $v . "'";
2023-01-11 14:40:12 +01:00
}
2023-01-19 18:37:48 +01:00
$sql = "SELECT * FROM " . $table . ((count($where) > 0) ? " WHERE ".$whereString.$orderString : "") .";";
2023-01-11 14:40:12 +01:00
try {
2023-01-19 18:37:48 +01:00
return $this->pdo->query($sql)->fetchAll();
2023-01-11 14:40:12 +01:00
}catch (PDOException $e){
die;
2023-01-11 11:02:42 +01:00
}
}
2022-12-21 10:14:44 +01:00
}