ordersprinter/gastsystem/php/dbutils.php

58 lines
1.4 KiB
PHP
Raw Normal View History

2020-11-19 23:10:06 +01:00
<?php
// Datenbank-Verbindungsparameter
require_once ('config.php');
class DbUtils {
public static function openDbAndReturnPdoStatic () {
$dsn = 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB;
$user = MYSQL_USER;
$password = MYSQL_PASSWORD;
$pdo = null;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
return $pdo;
}
public static function substTableAlias($sqlString) {
$out = str_replace("%ossystem%",TAB_PREFIX . 'ossystem',$sqlString);
$out = str_replace("%gueststatus%", TAB_PREFIX . 'gueststatus',$out);
$out = str_replace("%queue%", TAB_PREFIX . 'queue',$out);
return $out;
}
public static function getRowSqlObject($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
return ($stmt->fetchObject());
}
public static function fetchSqlAll($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
return ($stmt->fetchAll(PDO::FETCH_ASSOC));
}
public static function execSql($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
}
}