MINT-Projekt/class/DBClass.php

90 lines
2.4 KiB
PHP

<!DOCTYPE html>
<?php
/*
* Copyright (C) 2021 Eichehome
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Stellt eine Verbindung zu einer DB bereit
*
* @author Eichehome
*/
class DBClass extends stdClass
{
/**
* Zeiger auf die DB
* @var PDO
*/
private $pdo = null;
/**
* Das Abgrage-Statement
* @var PDOStatement
*/
private $stmt = null;
/**
* Verbindet mit Server und DB
* @param string $dbname Name der Datenbank
* @param string $user Benutzername
* @param string $passwort Passwort des Benutzers
* @param string $host Adresse des Datenbankservers
*/
public function __construct($dbname, $user, $passwort, $host)
{
try {
$this->pdo = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $user, $passwort);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
/**
* Führt eine SQL-Anweisung ohne Rückgabe eines Datensatzes aus
* @param string $querystring
* @param array $params
* @return boolean
*/
public function query($querystring, array $params = array())
{
try {
$this->stmt = $this->pdo->prepare($querystring);
return $this->stmt->execute($params);
} catch (PDOException $ex) {
$this->stmt = null;
} catch (Exception $ex) {
$this->stmt = null;
}
return false;
}
/**
* Führt eine SQL-Anweisung mit Rückgabe eines Datensatzes aus
* @param string $querystring
* @param array $params
* @return array
*/
public function select($querystring, array $params = array())
{
if ($this->query($querystring, $params)) {
return $this->stmt->fetchAll();
} else {
return array();
}
}
}