-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBClass.php
executable file
·66 lines (56 loc) · 1.42 KB
/
DBClass.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
<?php
//Connecting to MySQL database
//Use functions query, select and quote functions as and when needed
class DBClass
{
protected static $connection;
//Return mysqli object on success, false on failure
public function connect()
{
if(!isset(self::$connection))
{
$config = parse_ini_file('./config.ini');
self::$connection = new mysqli($config['host'],$config['username'],$config['password'],$config['dbname']);
}
if(self::$connection === false)
{
return false;
}
return self::$connection;
}
//Return mysqli_result object on success, false on failure
public function query($query)
{
$con = $this -> connect();
if($con===false)
die("Couldn't connect to database");
$result = $con -> query($query);
return $result;
}
//Return associative array of rows on success, false on failure
public function select($query)
{
$rows = array();
$result = $this -> query($query);
if($result === false) {
return false;
}
while ($row = $result -> fetch_assoc())
{
$rows[] = $row;
}
return $rows;
}
//Return last error from daatbase as string
public function error()
{
$con = $this -> connect();
return $con -> error;
}
//Create a legal SQL string for use in an SQL statement; Return the escaped string
public function quote($value) {
$connection = $this -> connect();
return "'" . $connection -> real_escape_string($value) . "'";
}
}
?>