-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseHelper.class.php
42 lines (38 loc) · 1.31 KB
/
DatabaseHelper.class.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
<?php
class DatabaseHelper {
// Create the connection to the database
public static function createConnectionInfo($values=array()) {
// pass in the connection string, username, and password as array
$connString = $values[0];
$user = $values[1];
$password = $values[2];
$pdo = new PDO($connString,$user,$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
// run an SQL query and return the cursor to the database
public static function runQuery($connection, $sql, $parameters=array()) {
// Ensure parameters are in an array
if (!is_array($parameters)) {
$parameters = array($parameters);
}
$statement = null;
if (count($parameters) > 0) {
// Use a prepared statement if parameters
$statement = $connection->prepare($sql);
$executedOk = $statement->execute($parameters);
if (! $executedOk) {
throw new PDOException;
}
}
else {
// Execute a normal query
$statement = $connection->query($sql);
if (!$statement) {
throw new PDOException;
}
}
return $statement;
}
}
?>