3 // MySQL database class
5 // For debugging, insert 'dbg(x);' anywhere in the program flow.
6 // x = 1: display db success/failure following content
7 // x = 2: display full queries following content
8 // x = 3: display full queries using echo; which will mess up display
9 // really bad but will return output in stubborn cases.
11 if(! class_exists('dba')) {
17 function __construct($server,$user,$pass,$db,$install = false) {
18 $this->db = @new mysqli($server,$user,$pass,$db);
19 if((mysqli_connect_errno()) && (! install))
23 public function q($sql) {
29 $result = @$this->db->query($sql);
35 if($this->db->mysqli->errno)
36 $debug_text .= $this->db->mysqli->error . EOL;
40 elseif($result === true)
43 $mesg = $result->num_rows.' results' . EOL;
45 $str = 'SQL = ' . $sql . EOL . 'SQL returned ' . $mesg . EOL;
47 switch($this->debug) {
57 if(($result === true) || ($result === false))
61 if($result->num_rows) {
62 while($x = $result->fetch_array(MYSQL_ASSOC))
64 $result->free_result();
68 $debug_text .= printable(print_r($r, true). EOL);
69 elseif($this->debug == 3)
70 echo printable(print_r($r, true) . EOL) ;
75 public function dbg($dbg) {
79 public function escape($str) {
80 return @$this->db->real_escape_string($str);
83 function __destruct() {
88 if(! function_exists('printable')) {
89 function printable($s) {
90 $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
91 return(escape_tags(str_replace("\x00",'.',$s)));
94 // Procedural functions
95 if(! function_exists('dbg')) {
96 function dbg($state) {
101 if(! function_exists('dbesc')) {
102 function dbesc($str) {
104 return($db->escape($str));
108 // Function: q($sql,$args);
109 // Description: execute SQL query with printf style args.
110 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
113 if(! function_exists('q')) {
117 $args = func_get_args();
119 $ret = $db->q(vsprintf($sql,$args));
124 // Caller is responsible for ensuring that any integer arguments to
125 // dbesc_array are actually integers and not malformed strings containing
126 // SQL injection vectors. All integer array elements should be specifically
127 // cast to int to avoid trouble.
130 if(! function_exists('dbesc_array_cb')) {
131 function dbesc_array_cb(&$item, $key) {
133 $item = dbesc($item);
137 if(! function_exists('dbesc_array')) {
138 function dbesc_array(&$arr) {
139 if(is_array($arr) && count($arr)) {
140 array_walk($arr,'dbesc_array_cb');