add: select

This commit is contained in:
Malte Schulze Hobeling 2023-01-11 11:02:42 +01:00
parent 85f49f1c8c
commit b0ef6fc88d

View File

@ -2,6 +2,9 @@
namespace BancaDati; namespace BancaDati;
use PDO;
use PDOException;
class BancaDati { class BancaDati {
private $dbName = "BancaDati"; private $dbName = "BancaDati";
private $linkName = "localhost"; private $linkName = "localhost";
@ -14,11 +17,11 @@ class BancaDati {
} }
private function linkDB() { private function linkDB() {
try { try {
$this->pdo = new \PDO("mysql:dbname=$this->dbName;host=$this->linkName" $this->pdo = new PDO("mysql:dbname=$this->dbName;host=$this->linkName"
, $this->user , $this->user
, $this->pw , $this->pw
, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)); , array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (\PDOException $e) { } catch (PDOException $e) {
die; die;
} }
} }
@ -51,7 +54,7 @@ class BancaDati {
try { try {
$sth = $this->pdo->prepare($sql); $sth = $this->pdo->prepare($sql);
$sth->execute(); $sth->execute();
}catch (\PDOException $e){ }catch (PDOException $e){
die; die;
} }
} }
@ -75,7 +78,7 @@ class BancaDati {
try { try {
$sth = $this->pdo->prepare($sql); $sth = $this->pdo->prepare($sql);
$sth->execute(); $sth->execute();
}catch (\PDOException $e){ }catch (PDOException $e){
die; die;
} }
} }
@ -92,8 +95,30 @@ class BancaDati {
try { try {
$sth = $this->pdo->prepare($sql); $sth = $this->pdo->prepare($sql);
$sth->execute(); $sth->execute();
}catch (\PDOException $e){ }catch (PDOException $e){
die; die;
} }
} }
/**
* Einheitliche select Funktion
* @param string $table
* @param array $selects
* @param array $data
* @return void
* @author Malte Schulze Hobeling
*/
public function select(string $table, array $selects, array $data){
$view = "";
foreach ($selects as $select){
$view .= $select . ",";
}
$view = trim($view,",");
$where = "";
foreach ($data as $col => $v) {
$where .= $col . "=" . $v . " AND ";
}
$where = trim($where,"AND ");
$sql = "SELECT ".$view." FROM ".$table." WHERE ".$where.";";
}
} }