aboutsummaryrefslogtreecommitdiffstats
path: root/library/ajaxchat/chat/lib/class/AJAXChatMySQLQuery.php
blob: f2f3fd466ab5c64e674fd8477a02ae39bfa863eb (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
87
88
89
<?php
/*
 * @package AJAX_Chat
 * @author Sebastian Tschan
 * @copyright (c) Sebastian Tschan
 * @license Modified MIT License
 * @link https://blueimp.net/ajax/
 */

// Class to perform SQL (MySQL) queries:
class AJAXChatMySQLQuery {

	var $_connectionID;
	var $_sql = '';
	var $_result = 0;
	var $_errno = 0;
	var $_error = '';

	// Constructor:
	function __construct($sql, $connectionID = null) {
		$this->_sql = trim($sql);
		$this->_connectionID = $connectionID;
		if($this->_connectionID) {
			$this->_result = mysql_query($this->_sql, $this->_connectionID);
			if(!$this->_result) {
				$this->_errno = mysql_errno($this->_connectionID);
				$this->_error = mysql_error($this->_connectionID);
			}
		} else {
			$this->_result = mysql_query($this->_sql);
			if(!$this->_result) {
				$this->_errno = mysql_errno();
				$this->_error = mysql_error();
			}	
		}
	}

	// Returns true if an error occured:
	function error() {
		// Returns true if the Result-ID is valid:
		return !(bool)($this->_result);
	}

	// Returns an Error-String:
	function getError() {
		if($this->error()) {
			$str  = 'Query: '	 .$this->_sql  ."\n";
			$str .= 'Error-Report: '	.$this->_error."\n";
			$str .= 'Error-Code: '.$this->_errno;
		} else {
			$str = "No errors.";
		}
		return $str;
	}

	// Returns the content:
	function fetch() {
		if($this->error()) {
			return null;
		} else {
			return mysql_fetch_assoc($this->_result);
		}
	}

	// Returns the number of rows (SELECT or SHOW):
	function numRows() {
		if($this->error()) {
			return null;
		} else {
			return mysql_num_rows($this->_result);
		}
	}

	// Returns the number of affected rows (INSERT, UPDATE, REPLACE or DELETE):
	function affectedRows() {
		if($this->error()) {
			return null;
		} else {
			return mysql_affected_rows($this->_connectionID);
		}
	}

	// Frees the memory:
	function free() {
		@mysql_free_result($this->_result);
	}
	
}
?>