DirektiveDesDons/BancaDati/BancaDati.php

127 lines
3.3 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) {
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-12 14:28:40 +01:00
$value .= "'" . $v . "'" . ",";
2022-12-21 11:19:40 +01:00
$column .= $col . ",";
}
$value = trim($value, ",");
$column = trim($column, ",");
2023-01-12 14:28:40 +01:00
$sql = "INSERT INTO $table ($column) VALUES ($value);";
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){
2023-01-12 14:28:40 +01:00
var_dump($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-12 15:09:08 +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){
$sql = "DELETE FROM " . $table . " 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 11:19:40 +01:00
die;
}
}
2023-01-11 11:02:42 +01:00
2023-01-11 14:40:12 +01:00
public function select(string $table, array $data, array $order = null){
2023-01-11 11:02:42 +01:00
$where = "";
foreach ($data as $col => $v) {
2023-01-11 14:40:12 +01:00
if($where != ""){
$where .= " AND ";
}
2023-01-12 14:36:37 +01:00
$where .= $col . "=" . "'" . $v . "'";
2023-01-11 14:40:12 +01:00
}
$sql = "SELECT * FROM ".$table." WHERE ".$where;
if(isset($order["by"])){
2023-01-11 14:41:30 +01:00
$sql .= " ORDER BY ".$order["by"];
2023-01-11 14:40:12 +01:00
}
if(isset($order["order"])){
$sql .= $order["order"];
}
try {
2023-01-12 14:41:51 +01:00
return $this->pdo->query($sql)->fetch();
2023-01-11 14:40:12 +01:00
}catch (PDOException $e){
2023-01-12 15:29:58 +01:00
var_dump($e);
2023-01-11 14:40:12 +01:00
die;
2023-01-11 11:02:42 +01:00
}
}
2022-12-21 10:14:44 +01:00
}