Combu Server  3.1.1
PHP API Documentation
Database.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class Database {
11 
15  protected $_dbType;
16  protected $_dbServer;
17  protected $_dbPort;
18  protected $_dbName;
19  protected $_dbUser;
20  protected $_dbPass;
21 
26  protected $_connection;
27 
31  public function __construct($dbType, $dbServer, $dbPort, $dbName, $dbUser, $dbPass) {
32  $this->_dbType = $dbType;
33  $this->_dbServer = $dbServer;
34  $this->_dbPort = $dbPort;
35  $this->_dbName = $dbName;
36  $this->_dbUser = $dbUser;
37  $this->_dbPass = $dbPass;
38  $this->_setConnection();
39  }
40 
45  public function GetConnection() {
46  return $this->_connection;
47  }
48 
52  protected function _setConnection() {
53  $this->_connection = mysqli_connect($this->_dbServer, $this->_dbUser, $this->_dbPass, $this->_dbName);
54  if ($this->_connection) {
55  $this->_selectDatabase();
56  } else {
57  $this->_connection = false;
58  }
59  }
60 
64  protected function _selectDatabase() {
65  mysqli_select_db($this->_connection, $this->_dbName);
66  //$this->Query("SET NAMES 'UTF8'");
67  }
68 
72  public function CloseConnection() {
73  if ($this->_connection) {
74  mysqli_close($this->_connection);
75  $this->_connection = null;
76  }
77  }
78 
83  public function TestConnection() {
84  if ($this->_connection) {
85  return mysqli_ping($this->_connection);
86  }
87  return FALSE;
88  }
89 
95  public function Query($query) {
96  if ($this->_connection && $query) {
97  return mysqli_query($this->_connection, $query);
98  }
99  return FALSE;
100  }
101 
106  public function InsertedId($name = NULL) {
107  if ($this->_connection) {
108  return mysqli_insert_id($this->_connection);
109  }
110  return 0;
111  }
112 
117  public function AffectedRows() {
118  return intval(mysqli_affected_rows($this->_connection));
119  }
120 
129  private function _Fetch($result, $result_type) {
130  if ($result) {
131  return mysqli_fetch_array($result, $result_type);
132  }
133  return NULL;
134  }
135 
143  public function FetchAssoc($result) {
144  return $this->_Fetch($result, MYSQLI_ASSOC);
145  }
146 
154  public function FetchNum($result) {
155  return $this->_Fetch($result, MYSQLI_NUM);
156  }
157 
163  public function Escape($text) {
164  return mysqli_real_escape_string($this->_connection, $text);
165  }
166 
172  public function EscapeDate($text) {
173  if (!$text || $text == '') {
174  return "NULL";
175  }
176  if (strpos($text, ":") === false) {
177  return "'" . date("Y-m-d", Utils::GetTimestamp($text)) . "'";
178  }
179  return "'" . date("Y-m-d H:i:s", Utils::GetTimestamp($text)) . "'";
180  }
181 
187  public function RecordExists($query) {
188  if ($this->_connection && $query) {
189  $res = $this->Query($query);
190  $row = $this->FetchAssoc($res);
191  if ($row) {
192  return TRUE;
193  }
194  }
195  return FALSE;
196  }
197 
202  public function TransactionStart() {
203  if ($this->_connection) {
204  return mysqli_begin_transaction($this->_connection);
205  }
206  return FALSE;
207  }
208 
213  public function TransactionCommit() {
214  if ($this->_connection) {
215  return mysqli_commit($this->_connection);
216  }
217  return FALSE;
218  }
219 
224  public function TransactionRollback() {
225  if ($this->_connection) {
226  return mysqli_rollback($this->_connection);
227  }
228  return false;
229  }
230 
235  public function GetError() {
236  return mysqli_error($this->_connection);
237  }
238 
243  public function GetErrorNo() {
244  return mysqli_errno($this->_connection);
245  }
246 
247 }
EscapeDate($text)
Definition: Database.php:172
Query($query)
Definition: Database.php:95
RecordExists($query)
Definition: Database.php:187
Escape($text)
Definition: Database.php:163
__construct($dbType, $dbServer, $dbPort, $dbName, $dbUser, $dbPass)
Definition: Database.php:31
Definition: Account.php:3
FetchAssoc($result)
Definition: Database.php:143
FetchNum($result)
Definition: Database.php:154
InsertedId($name=NULL)
Definition: Database.php:106