Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mysqli.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Db/Statement.php';
28 
29 
40 {
41 
47  protected $_keys;
48 
54  protected $_values;
55 
59  protected $_meta = null;
60 
66  public function _prepare($sql)
67  {
68  $mysqli = $this->_adapter->getConnection();
69 
70  $this->_stmt = $mysqli->prepare($sql);
71 
72  if ($this->_stmt === false || $mysqli->errno) {
76  #require_once 'Zend/Db/Statement/Mysqli/Exception.php';
77  throw new Zend_Db_Statement_Mysqli_Exception("Mysqli prepare error: " . $mysqli->error, $mysqli->errno);
78  }
79  }
80 
92  protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
93  {
94  return true;
95  }
96 
102  public function close()
103  {
104  if ($this->_stmt) {
105  $r = $this->_stmt->close();
106  $this->_stmt = null;
107  return $r;
108  }
109  return false;
110  }
111 
117  public function closeCursor()
118  {
119  if ($stmt = $this->_stmt) {
120  $mysqli = $this->_adapter->getConnection();
121  while ($mysqli->more_results()) {
122  $mysqli->next_result();
123  }
124  $this->_stmt->free_result();
125  return $this->_stmt->reset();
126  }
127  return false;
128  }
129 
136  public function columnCount()
137  {
138  if (isset($this->_meta) && $this->_meta) {
139  return $this->_meta->field_count;
140  }
141  return 0;
142  }
143 
150  public function errorCode()
151  {
152  if (!$this->_stmt) {
153  return false;
154  }
155  return substr($this->_stmt->sqlstate, 0, 5);
156  }
157 
164  public function errorInfo()
165  {
166  if (!$this->_stmt) {
167  return false;
168  }
169  return array(
170  substr($this->_stmt->sqlstate, 0, 5),
171  $this->_stmt->errno,
172  $this->_stmt->error,
173  );
174  }
175 
183  public function _execute(array $params = null)
184  {
185  if (!$this->_stmt) {
186  return false;
187  }
188 
189  // if no params were given as an argument to execute(),
190  // then default to the _bindParam array
191  if ($params === null) {
193  }
194  // send $params as input parameters to the statement
195  if ($params) {
196  array_unshift($params, str_repeat('s', count($params)));
197  $stmtParams = array();
198  foreach ($params as $k => &$value) {
199  $stmtParams[$k] = &$value;
200  }
201  call_user_func_array(
202  array($this->_stmt, 'bind_param'),
203  $stmtParams
204  );
205  }
206 
207  // execute the statement
208  $retval = $this->_stmt->execute();
209  if ($retval === false) {
213  #require_once 'Zend/Db/Statement/Mysqli/Exception.php';
214  throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement execute error : " . $this->_stmt->error, $this->_stmt->errno);
215  }
216 
217 
218  // retain metadata
219  if ($this->_meta === null) {
220  $this->_meta = $this->_stmt->result_metadata();
221  if ($this->_stmt->errno) {
225  #require_once 'Zend/Db/Statement/Mysqli/Exception.php';
226  throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error: " . $this->_stmt->error, $this->_stmt->errno);
227  }
228  }
229 
230  // statements that have no result set do not return metadata
231  if ($this->_meta !== false) {
232 
233  // get the column names that will result
234  $this->_keys = array();
235  foreach ($this->_meta->fetch_fields() as $col) {
236  $this->_keys[] = $this->_adapter->foldCase($col->name);
237  }
238 
239  // set up a binding space for result variables
240  $this->_values = array_fill(0, count($this->_keys), null);
241 
242  // set up references to the result binding space.
243  // just passing $this->_values in the call_user_func_array()
244  // below won't work, you need references.
245  $refs = array();
246  foreach ($this->_values as $i => &$f) {
247  $refs[$i] = &$f;
248  }
249 
250  $this->_stmt->store_result();
251  // bind to the result variables
252  call_user_func_array(
253  array($this->_stmt, 'bind_result'),
254  $this->_values
255  );
256  }
257  return $retval;
258  }
259 
260 
270  public function fetch($style = null, $cursor = null, $offset = null)
271  {
272  if (!$this->_stmt) {
273  return false;
274  }
275  // fetch the next result
276  $retval = $this->_stmt->fetch();
277  switch ($retval) {
278  case null: // end of data
279  case false: // error occurred
280  $this->_stmt->reset();
281  return false;
282  default:
283  // fallthrough
284  }
285 
286  // make sure we have a fetch mode
287  if ($style === null) {
288  $style = $this->_fetchMode;
289  }
290 
291  // dereference the result values, otherwise things like fetchAll()
292  // return the same values for every entry (because of the reference).
293  $values = array();
294  foreach ($this->_values as $key => $val) {
295  $values[] = $val;
296  }
297 
298  $row = false;
299  switch ($style) {
300  case Zend_Db::FETCH_NUM:
301  $row = $values;
302  break;
304  $row = array_combine($this->_keys, $values);
305  break;
306  case Zend_Db::FETCH_BOTH:
307  $assoc = array_combine($this->_keys, $values);
308  $row = array_merge($values, $assoc);
309  break;
310  case Zend_Db::FETCH_OBJ:
311  $row = (object) array_combine($this->_keys, $values);
312  break;
314  $assoc = array_combine($this->_keys, $values);
315  $row = array_merge($values, $assoc);
316  return $this->_fetchBound($row);
317  break;
318  default:
322  #require_once 'Zend/Db/Statement/Mysqli/Exception.php';
323  throw new Zend_Db_Statement_Mysqli_Exception("Invalid fetch mode '$style' specified");
324  break;
325  }
326  return $row;
327  }
328 
337  public function nextRowset()
338  {
342  #require_once 'Zend/Db/Statement/Mysqli/Exception.php';
343  throw new Zend_Db_Statement_Mysqli_Exception(__FUNCTION__.'() is not implemented');
344  }
345 
353  public function rowCount()
354  {
355  if (!$this->_adapter) {
356  return false;
357  }
358  $mysqli = $this->_adapter->getConnection();
359  return $mysqli->affected_rows;
360  }
361 
362 }
_bindParam($parameter, &$variable, $type=null, $length=null, $options=null)
Definition: Mysqli.php:92
const FETCH_BOUND
Definition: Db.php:144
const FETCH_ASSOC
Definition: Db.php:142
$values
Definition: options.phtml:88
_execute(array $params=null)
Definition: Mysqli.php:183
$variable
Definition: variable.php:7
$type
Definition: item.phtml:13
const FETCH_BOTH
Definition: Db.php:143
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
const FETCH_OBJ
Definition: Db.php:154
fetch($style=null, $cursor=null, $offset=null)
Definition: Mysqli.php:270
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31