-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_utils.php
40 lines (36 loc) · 1.1 KB
/
db_utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace AkadChat\Dbutils;
require "settings.php";
function connect() {
$dbConnection = false;
$params = sprintf("host=". \AkadChat\Settings\dbHost ." dbname=". \AkadChat\Settings\dbName ." user=". \AkadChat\Settings\dbUser ." password=". \AkadChat\Settings\dbPassword ." connect_timeout=5");
$dbConnection = pg_connect($params);
if (!$dbConnection) {
$error = pg_last_error();
$msg = "Verbindungsaufbau zu \"$params\" fehlgeschlagen: $error";
throw new \Exception($msg);
}
return $dbConnection;
}
function close($dbConnection) {
if ($dbConnection != null) {
pg_close($dbConnection);
}
}
function queryParams($dbConnection, $query, $params) {
$result = false;
if ($params == null) {
$params = [];
}
$result = pg_query_params($dbConnection, $query, $params);
if (!$result) {
$error = pg_last_error();
$msg = "Query \"$query\" mit Parametern \"$params\" fehlgeschlagen: $error";
throw new \Exception($msg);
}
return $result;
}
function freeResult($result) {
pg_free_result($result);
}
?>