aboutsummaryrefslogtreecommitdiffstats
path: root/include/dba/dba_mysqli.php
blob: 57a7559a1eef55f104cd4e367c81a59736a67483 (plain) (blame)
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
82
83
84
85
86
<?php /** @file */

require_once('include/dba/dba_driver.php');

class dba_mysqli extends dba_driver {

	function connect($server,$port,$user,$pass,$db) {
		if($port)
			$this->db = new mysqli($server,$user,$pass,$db, $port);
		else
			$this->db = new mysqli($server,$user,$pass,$db);

		if($this->db->connect_error) {
			$this->connected = false;
			$this->error = $this->db->connect_error;

			if(file_exists('dbfail.out')) {
				file_put_contents('dbfail.out', datetime_convert() . "\nConnect: " . $this->error . "\n", FILE_APPEND);
			}

			return false;
		}
		else {
			$this->connected = true;
			return true;
		}
	}

	function q($sql) {
		if((! $this->db) || (! $this->connected))
			return false;

		$this->error = '';
		$result = $this->db->query($sql);

		if($this->db->errno)
			$this->error = $this->db->error;


		if($this->error) {
			logger('dba_mysqli: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR);
			if(file_exists('dbfail.out')) {
				file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND);
			}
		}

		if(($result === true) || ($result === false)) {
			if($this->debug) {
				logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL,(($result) ? LOG_INFO : LOG_ERR));
			}
			return $result;
		}

		if($this->debug) {
			logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returned ' . $result->num_rows . ' results.', LOGGER_NORMAL, LOG_INFO); 
		}

		$r = array();
		if($result->num_rows) {
			while($x = $result->fetch_array(MYSQLI_ASSOC))
				$r[] = $x;
			$result->free_result();
			if($this->debug) {
				logger('dba_mysqli: ' . printable(print_r($r,true)), LOGGER_NORMAL, LOG_INFO);
			}
		}
		return $r;
	}

	function escape($str) {
		if($this->db && $this->connected) {
			return @$this->db->real_escape_string($str);
		}
	}

	function close() {
		if($this->db)
			$this->db->close();
		$this->connected = false;
	}
	
	function getdriver() {
		return 'mysqli';
	}

}