diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2972513 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "intelephense.environment.phpVersion": "7.4.33" +} \ No newline at end of file diff --git a/database.php b/database.php index 27b07e0..ac61ca3 100644 --- a/database.php +++ b/database.php @@ -1,15 +1,35 @@ false)); - - $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); - - return $db; +class DB extends PDO +{ + public function __construct($dsn, $username = null, $password = null, array $options = null) + { + parent::__construct($dsn, $username, $password, $options); + } + + static function openConnection() + { + $conn = null; + try { + $dataAccess = parse_ini_file("configs/db.ini", true); + + $conn = new PDO( + "mysql:host=" . $dataAccess["ZEDAT"]["host"] . + ";dbname=" . $dataAccess["ZEDAT"]["database"] . ";charset=utf8", + $dataAccess["ZEDAT"]["user"], + $dataAccess["ZEDAT"]["passwd"], + array(PDO::ATTR_PERSISTENT => false) + ); + + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + } catch (PDOException $exc) { + echo $exc->getMessage(); + } + return $conn; + } + + static function closeConnection(&$conn) + { + $conn = null; + } } -?> diff --git a/errors.php b/errors.php new file mode 100644 index 0000000..893a814 --- /dev/null +++ b/errors.php @@ -0,0 +1,3 @@ +prepare($query); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); -echo json_encode($result); -?> \ No newline at end of file +DB::closeConnection($db); + +echo json_encode($result); diff --git a/phpCourse.code-workspace b/phpCourse.code-workspace new file mode 100644 index 0000000..362d7c2 --- /dev/null +++ b/phpCourse.code-workspace @@ -0,0 +1,7 @@ +{ + "folders": [ + { + "path": "." + } + ] +} \ No newline at end of file