aboutsummaryrefslogtreecommitdiffstats
path: root/include/dba_mysql.php
blob: f3622ee8edf5509e70834a10f7b94ddecba311b2 (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
<?php

require_once('include/dba_driver.php');


abstract class dba_mysql extends dba_driver {

	protected function connect($server,$user,$pass,$db) {
		$this->db = mysql_connect($server,$user,$pass);
		if($this->db && mysql_select_db($db,$this->db)) {
			$this->connected = true;
		}
		if($this->connected) {
			return true;
		}
		return false;
	}


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

		$this->error = '';
		$result = @mysql_query($sql,$this->db);


		if(mysql_errno($this->db))
			$this->error = mysql_error($this->db);

		if($result === false || $this->error) {
			logger('dba_mysql: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
			if(file_exists('dbfail.out'))
				file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
		}

		if(($result === true) || ($result === false))
			return $result;

		$r = array();
		if(mysql_num_rows($result)) {
			while($x = mysql_fetch_array($result,MYSQL_ASSOC))
				$r[] = $x;
			mysql_free_result($result);
			if($this->debug)
				logger('dba_mysql: ' . printable(print_r($r,true)));
		}
		return $r;
	}

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

	protected function close() {
		if($this->db)
			mysql_close($this->db);
		$this->connected = false;
	}

}