-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.php
executable file
·77 lines (70 loc) · 1.54 KB
/
connect.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
class DatabaseHandler
{
private $dsn;
private $username;
private $password;
private $pdo;
public function __construct($dsn, $username, $password)
{
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
}
public function connect()
{
$this->pdo = new PDO($this->dsn, $this->username, $this->password);
}
public function disconnect()
{
$this->pdo = null;
}
public function executeQuery($sql, $attributes)
{
$statement = $this->pdo->prepare($sql);
$success = $statement->execute($attributes);
if (!$success)
{
$errorInfo = $statement->errorInfo();
$errorMessage = $errorInfo[2];
throw new PDOException($errorMessage);
}
return $success;
}
public function selectQuery($sql, $attributes = [])
{
try
{
if (!empty($attributes))
{
$statement = $this->pdo->prepare($sql);
if (!$statement)
{
$errorInfo = $this->pdo->errorInfo();
throw new Exception("Error preparing the statement: " . $errorInfo[2]);
}
if (!$statement->execute($attributes))
{
$errorInfo = $statement->errorInfo();
throw new Exception("Error executing the prepared statement: " . $errorInfo[2]);
}
return $statement->fetchAll();
}
else
{
$result = $this->pdo->query($sql);
if (!$result)
{
$errorInfo = $this->pdo->errorInfo();
throw new Exception("Error executing the query: " . $errorInfo[2]);
}
return $result->fetchAll();
}
}
catch (Exception $e)
{
echo "Exception occurred: " . $e->getMessage();
}
return [];
}
}