-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_model.php
81 lines (67 loc) · 1.65 KB
/
conn_model.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
78
79
80
81
<?php
class Db{
//database conn
protected static $conn;
private function connect(){
//try and connect to database
if(!isset(self::$conn)){
//Load configuration as an array
$config = parse_ini_file('./config.ini'); //input .ini file
//get details from .ini file
$uname = $config['username'];
$pass = $config['password'];
$dbname = $config['dbname'];
$host = $config['host'];
self::$conn = new mysqli($host,$uname,$pass,$dbname);
}
//check if conn is true. --successful conn
if(self::$conn===false){
echo "connection not Successful. Check configuration file for correct details";
return false;
}
return self::$conn;
}
private function query($query){
//make the conn call
$conn = $this->connect();
//Query database
$result = $conn->query($query);
return $result;
}
private function error(){
return $this->connect()->error;
}
public function select($query){
$rows = array();
$result = $this->query($query);
if($result === false){
echo "No result found check query";
return false;
}
while($row=$result->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
public function insert($query){
$result = $this->query($query);
if($result === false){
echo $this->error();
}else{
return true;
}
}
public function remove($query){
$result = $this->query($query);
if($result === false){
echo $this->error();
}else{
return true;
}
}
//escape string
public function escapeString($value){
$conn = $this->connect();
return "'".$conn->real_escape_string($value)."'";
}
}