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 .= print_r($r, true). EOL;
69 // $debug_text .= quoted_printable_encode(print_r($r, true). EOL);
70 elseif($this->debug == 3)
71 echo print_r($r, true) . EOL ;
72 // echo quoted_printable_encode(print_r($r, true) . EOL) ;
77 public function dbg($dbg) {
81 public function escape($str) {
82 return @$this->db->real_escape_string($str);
85 function __destruct() {
90 // Procedural functions
91 if(! function_exists('dbg')) {
92 function dbg($state) {
97 if(! function_exists('dbesc')) {
98 function dbesc($str) {
100 return($db->escape($str));
104 // Function: q($sql,$args);
105 // Description: execute SQL query with printf style args.
106 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
109 if(! function_exists('q')) {
113 $args = func_get_args();
115 $ret = $db->q(vsprintf($sql,$args));
120 // Caller is responsible for ensuring that any integer arguments to
121 // dbesc_array are actually integers and not malformed strings containing
122 // SQL injection vectors. All integer array elements should be specifically
123 // cast to int to avoid trouble.
126 if(! function_exists('dbesc_array_cb')) {
127 function dbesc_array_cb(&$item, $key) {
129 $item = dbesc($item);
133 if(! function_exists('dbesc_array')) {
134 function dbesc_array(&$a) {
135 if(is_array($a) && count($a)) {
136 array_walk($a,'dbesc_array_cb');