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;
use PDO;
use PDOException;
class BancaDati {
private $dbName = "BancaDati";
private $linkName = "localhost";
@ -14,11 +17,11 @@ class BancaDati {
}
private function linkDB() {
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->pw
, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
} catch (\PDOException $e) {
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
die;
}
}
@ -51,7 +54,7 @@ class BancaDati {
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
}catch (\PDOException $e){
}catch (PDOException $e){
die;
}
}
@ -75,7 +78,7 @@ class BancaDati {
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
}catch (\PDOException $e){
}catch (PDOException $e){
die;
}
}
@ -92,8 +95,30 @@ class BancaDati {
try {
$sth = $this->pdo->prepare($sql);
$sth->execute();
}catch (\PDOException $e){
}catch (PDOException $e){
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.";";
}
}