-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_conn.class.php
More file actions
43 lines (29 loc) · 813 Bytes
/
db_conn.class.php
File metadata and controls
43 lines (29 loc) · 813 Bytes
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
<?php
//db connection class using singleton pattern
class dbConn{
//variable to hold connection object.
protected static $db;
private function __construct() {
$file = 'sample.sqlite';
try {
// assign PDO object to db variable
self::$db = new PDO( 'sqlite:' . $file );
//enable error mode
self::$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch (PDOException $e) {
//catch and display error
echo "Connection Error: " . $e->getMessage();
}
}
// get connection function.
public static function getConnection() {
//if connection doesnt exist already, create one.
if (!self::$db) {
new dbConn();
}
//return connection.
return self::$db;
}
}//end class
?>