-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB.php
More file actions
53 lines (46 loc) · 1.57 KB
/
DB.php
File metadata and controls
53 lines (46 loc) · 1.57 KB
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
<?
//SH management - API - MySQL DB interface
//Provides MySQL connectivity to other objects
class DB {
private static $instance;
private $link;
public static $querycount=0;
public static $querytime=0;
public static $queries=array();
//return link
public static function get() {
global $config;
if(!self::$instance)
self::$instance=new self($config["db"]["host"],$config["db"]["user"],$config["db"]["pass"],$config["db"]["db"]);
return self::$instance;
}
private function __construct($host,$user,$pass,$db) {
// /* t */ printf("Opening link to %s:%s@%s/%s",$user,$pass,$host,$db);
$this->link=new mysqli($host,$user,$pass,$db);
if($this->link->connect_error)
throw new Exception("MySQL connect failed: %s (%d)",$this->link->connect_error,$this->link->connect_errno);
$ret=$this->link->set_charset("utf8");
if($ret===false)
throw new Exception("MySQL set_charset failed: %s (%d)",$this->link->error,$this->link->errno);
}
public function getLink() {
return $this->link;
}
public function getError() {
return sprintf("%s (%d)",$this->link->error,$this->link->errno);
}
public static function esc($str) {
return self::get()->link->real_escape_string($str);
}
//get an array of the columns in a table
public static function getTableCols($table) {
$q=new DB_Query("SHOW COLUMNS FROM `".self::esc($table)."`");
if($q->numRows<1)
printf("Table %s has no rows?!",$table);
$cols=array();
while($e=$q->fetch())
$cols[]=$e["Field"];
$q->free();
return $cols;
}
}