XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
DBMysqli.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
4 require_once('DBMysql.class.php');
5 
16 class DBMysqli extends DBMysql
17 {
18 
23  function __construct($auto_connect = TRUE)
24  {
25  $this->_setDBInfo();
26  if($auto_connect) $this->_connect();
27  }
28 
33  function create()
34  {
35  return new DBMysqli;
36  }
37 
45  {
46  // Attempt to connect
47  if($connection["db_port"])
48  {
49  $result = @mysqli_connect($connection["db_hostname"]
50  , $connection["db_userid"]
51  , $connection["db_password"]
52  , $connection["db_database"]
53  , $connection["db_port"]);
54  }
55  else
56  {
57  $result = @mysqli_connect($connection["db_hostname"]
58  , $connection["db_userid"]
59  , $connection["db_password"]
60  , $connection["db_database"]);
61  }
62  $error = mysqli_connect_errno();
63  if($error)
64  {
65  $this->setError($error, mysqli_connect_error());
66  return;
67  }
68  mysqli_set_charset($result, 'utf8');
69  return $result;
70  }
71 
78  function _close($connection)
79  {
80  mysqli_close($connection);
81  }
82 
88  function addQuotes($string)
89  {
90  if(version_compare(PHP_VERSION, "5.4.0", "<") && get_magic_quotes_gpc())
91  {
92  $string = stripslashes(str_replace("\\", "\\\\", $string));
93  }
94  if(!is_numeric($string))
95  {
96  $connection = $this->_getConnection('master');
97  $string = mysqli_escape_string($connection, $string);
98  }
99  return $string;
100  }
101 
110  {
111  if($this->use_prepared_statements == 'Y')
112  {
113  // 1. Prepare query
114  $stmt = mysqli_prepare($connection, $query);
115  if($stmt)
116  {
117  $types = '';
118  $params = array();
119  $this->_prepareQueryParameters($types, $params);
120 
121  if(!empty($params))
122  {
123  $args[0] = $stmt;
124  $args[1] = $types;
125 
126  $i = 2;
127  foreach($params as $key => $param)
128  {
129  $copy[$key] = $param;
130  $args[$i++] = &$copy[$key];
131  }
132 
133  // 2. Bind parameters
134  $status = call_user_func_array('mysqli_stmt_bind_param', $args);
135  if(!$status)
136  {
137  $this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection));
138  }
139  }
140 
141  // 3. Execute query
142  $status = mysqli_stmt_execute($stmt);
143 
144  if(!$status)
145  {
146  $this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection));
147  }
148 
149  // Return stmt for other processing - like retrieving resultset (_fetch)
150  return $stmt;
151  // mysqli_stmt_close($stmt);
152  }
153  }
154  // Run the query statement
155  $result = mysqli_query($connection, $query);
156  // Error Check
157  $error = mysqli_error($connection);
158  if($error)
159  {
160  $this->setError(mysqli_errno($connection), $error);
161  }
162  // Return result
163  return $result;
164  }
165 
173  function _prepareQueryParameters(&$types, &$params)
174  {
175  $types = '';
176  $params = array();
177  if(!$this->param)
178  {
179  return;
180  }
181 
182  foreach($this->param as $k => $o)
183  {
184  $value = $o->getUnescapedValue();
185  $type = $o->getType();
186 
187  // Skip column names -> this should be concatenated to query string
188  if($o->isColumnName())
189  {
190  continue;
191  }
192 
193  switch($type)
194  {
195  case 'number' :
196  $type = 'i';
197  break;
198  case 'varchar' :
199  $type = 's';
200  break;
201  default:
202  $type = 's';
203  }
204 
205  if(is_array($value))
206  {
207  foreach($value as $v)
208  {
209  $params[] = $v;
210  $types .= $type;
211  }
212  }
213  else
214  {
215  $params[] = $value;
216  $types .= $type;
217  }
218  }
219  }
220 
227  function _fetch($result, $arrayIndexEndValue = NULL)
228  {
229  if($this->use_prepared_statements != 'Y')
230  {
231  return parent::_fetch($result, $arrayIndexEndValue);
232  }
233  $output = array();
234  if(!$this->isConnected() || $this->isError() || !$result)
235  {
236  return $output;
237  }
238 
239  // Prepared stements: bind result variable and fetch data
240  $stmt = $result;
241  $meta = mysqli_stmt_result_metadata($stmt);
242  $fields = mysqli_fetch_fields($meta);
243 
249  $longtext_exists = false;
250  foreach($fields as $field)
251  {
252  if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails
253  {
254  $field->name = 'repeat_' . $field->name;
255  }
256 
257  // Array passed needs to contain references, not values
258  $row[$field->name] = "";
259  $resultArray[$field->name] = &$row[$field->name];
260 
261  if($field->type == 252)
262  {
263  $longtext_exists = true;
264  }
265  }
266  $resultArray = array_merge(array($stmt), $resultArray);
267 
268  if($longtext_exists)
269  {
270  mysqli_stmt_store_result($stmt);
271  }
272 
273  call_user_func_array('mysqli_stmt_bind_result', $resultArray);
274 
275  $rows = array();
276  while(mysqli_stmt_fetch($stmt))
277  {
278  $resultObject = new stdClass();
279 
280  foreach($resultArray as $key => $value)
281  {
282  if($key === 0)
283  {
284  continue; // Skip stmt object
285  }
286  if(strpos($key, 'repeat_'))
287  {
288  $key = substr($key, 6);
289  }
290  $resultObject->$key = $value;
291  }
292 
293  $rows[] = $resultObject;
294  }
295 
296  mysqli_stmt_close($stmt);
297 
298  if($arrayIndexEndValue)
299  {
300  foreach($rows as $row)
301  {
302  $output[$arrayIndexEndValue--] = $row;
303  }
304  }
305  else
306  {
307  $output = $rows;
308  }
309 
310  if(count($output) == 1)
311  {
312  if(isset($arrayIndexEndValue))
313  {
314  return $output;
315  }
316  else
317  {
318  return $output[0];
319  }
320  }
321 
322  return $output;
323  }
324 
331  function _executeInsertAct($queryObject, $with_values = false)
332  {
333  if($this->use_prepared_statements != 'Y')
334  {
335  return parent::_executeInsertAct($queryObject);
336  }
337  $this->param = $queryObject->getArguments();
338  $result = parent::_executeInsertAct($queryObject, $with_values);
339  unset($this->param);
340  return $result;
341  }
342 
349  function _executeUpdateAct($queryObject, $with_values = false)
350  {
351  if($this->use_prepared_statements != 'Y')
352  {
353  return parent::_executeUpdateAct($queryObject);
354  }
355  $this->param = $queryObject->getArguments();
356  $result = parent::_executeUpdateAct($queryObject, $with_values);
357  unset($this->param);
358  return $result;
359  }
360 
367  function _executeDeleteAct($queryObject, $with_values = false)
368  {
369  if($this->use_prepared_statements != 'Y')
370  {
371  return parent::_executeDeleteAct($queryObject);
372  }
373  $this->param = $queryObject->getArguments();
374  $result = parent::_executeDeleteAct($queryObject, $with_values);
375  unset($this->param);
376  return $result;
377  }
378 
388  function _executeSelectAct($queryObject, $connection = null, $with_values = false)
389  {
390  if($this->use_prepared_statements != 'Y')
391  {
392  return parent::_executeSelectAct($queryObject, $connection);
393  }
394  $this->param = $queryObject->getArguments();
395  $result = parent::_executeSelectAct($queryObject, $connection, $with_values);
396  unset($this->param);
397  return $result;
398  }
399 
406  function db_insert_id()
407  {
408  $connection = $this->_getConnection('master');
409  return mysqli_insert_id($connection);
410  }
411 
418  {
419  return mysqli_fetch_object($result);
420  }
421 
428  {
429  return mysqli_free_result($result);
430  }
431 
432 }
433 
434 DBMysqli::$isSupported = function_exists('mysqli_connect');
435 
436 /* End of file DBMysqli.class.php */
437 /* Location: ./classes/db/DBMysqli.class.php */
setError($errno=0, $errstr= 'success')
Definition: DB.class.php:476
__query($query, $connection)
_executeSelectAct($queryObject, $connection=null, $with_values=false)
static $isSupported
Definition: DB.class.php:19
$output
Definition: ko.install.php:193
isConnected($type= 'master', $indx=0)
Definition: DB.class.php:360
_fetch($result, $arrayIndexEndValue=NULL)
_executeInsertAct($queryObject, $with_values=false)
$query
Definition: DB.class.php:84
$args
Definition: ko.install.php:185
isError()
Definition: DB.class.php:486
db_fetch_object(&$result)
addQuotes($string)
__connect($connection)
db_free_result(&$result)
_executeUpdateAct($queryObject, $with_values=false)
_prepareQueryParameters(&$types, &$params)
__construct($auto_connect=TRUE)
_connect($type= 'master', $indx=0)
Definition: DB.class.php:1266
_getConnection($type= 'master', $indx=NULL)
Definition: DB.class.php:1001
_executeDeleteAct($queryObject, $with_values=false)
_close($connection)
$result
Definition: DB.class.php:66
_setDBInfo()
Definition: DB.class.php:1216
$connection
Definition: DB.class.php:85