From b0ef6fc88d3ef22617db69214163b8ca4d8059da Mon Sep 17 00:00:00 2001 From: Malte Schulze Hobeling Date: Wed, 11 Jan 2023 11:02:42 +0100 Subject: [PATCH] add: select --- BancaDati/BancaDati.php | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/BancaDati/BancaDati.php b/BancaDati/BancaDati.php index d68cc7d..c508566 100644 --- a/BancaDati/BancaDati.php +++ b/BancaDati/BancaDati.php @@ -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.";"; + } } \ No newline at end of file