-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
47 lines (38 loc) · 1.22 KB
/
db.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
<?php
//define a database connection class
require_once("config.php");
class MySQLDB {
private $dbConn;
public function open_connection() {
//establish a connection to the database
$this->dbConn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//error checking
if(mysqli_connect_errno()) {
die( "Database connection error: ".mysqli_connect_error()."(".mysqli_connect_errno().")" );
}
}
public function close_connection() {
if(isset($this->dbConn)) {
mysqli_close($this->dbConn);
unset($this->dbConn);
}
}
public function query($sql) {
$result = mysqli_query($this->dbConn, $sql);
//error checking
if(!$result) {
die("Database query error: ".mysqli_error($this->dbConn)." (".mysqli_errno($this->dbConn).")");
}
if(strpos($sql, 'insert') || strpos($sql, 'delete') || strpos($sql, 'update')) {
$result = mysqli_affected_rows($this->dbconn);
return $result;
}
return $result;
}
//constructor method
function __construct(){
$this->open_connection();
}
}//end of class MySQLDB
$mydb = new MySQLDB();
?>