/
DBConn.php
65 lines (50 loc) · 1.99 KB
/
DBConn.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
<?php
class DBConn {
private static $db_conn;
public static function get_instance() {
if(isset(self::$db_conn)) {
return self::$db_conn;
} else {
require __DIR__.'\config.php';
global $hostname, $username, $password, $dbname;
self::$db_conn = mysqli_connect($hostname, $username, $password);
mysqli_select_db(self::$db_conn, $dbname);
return self::$db_conn;
}
}
public static function get_rows($table, $fields, $where) {
$sql_fields = (is_array($fields) && count($fields)>0)?implode(", ", $fields):" * ";
$sql_where = (is_array($where) && count($where)>0)?implode(" AND ", $where):" 1=1 ";
$sql = "SELECT ".$sql_fields." FROM ".$table
." WHERE ".$sql_where;
$db_conn = self::get_instance();
$res = mysqli_query($db_conn, $sql);
$result = array();
while($record = mysqli_fetch_assoc($res)) {
$result[] = $record;
}
return $result;
}
public static function add_row($table, $values) {
$sql_fields = implode(", ", array_keys($values));
$sql_values = implode("', '", $values);
$sql = "INSERT INTO ".$table." "
. " (".$sql_fields.")"
. " VALUES ('".$sql_values."')";
$db_conn = self::get_instance();
mysqli_query($db_conn, $sql);
}
public static function update_row($table, $values, $where) {
$update_val = array();
foreach($values as $key=>$val) {
$update_val[] = $key." = '".$val."'";
}
$sql_update_val = implode(", ", $update_val);
$sql_where = (is_array($where) && count($where)>0)?implode(" AND ", $where):" 1=1 ";
$sql = "UPDATE ".$table. " SET ".$sql_update_val
." WHERE ".$sql_where;
$db_conn = self::get_instance();
mysqli_query($db_conn, $sql);
}
}
?>