/** * Fetch for non-mysqlnd environments * * @todo review support for custom classes * @todo review pros/cons of using store_result() * @todo fix statements using AS * * @param $int_fetch_mode * @return array|null|object|\stdClass */ private function fetchOldSchool($int_fetch_mode) { $this->obj_stmt->store_result(); $obj_meta = $this->obj_stmt->result_metadata(); $arr_fields = $obj_meta->fetch_fields(); $obj_result = NULL !== $this->str_result_class ? new $this->str_result_class() : new \stdClass(); $arr_bind_fields = array(); foreach ($arr_fields as $obj_field) { $arr_bind_fields[] =& $obj_result->{$obj_field->name}; } call_user_func_array(array($this->obj_stmt, 'bind_result'), $arr_bind_fields); if (DB::FETCH_MODE_ONE === $int_fetch_mode) { if ($this->obj_stmt->fetch()) { $mix_data = $obj_result; } else { $mix_data = NULL; } } else { $mix_data = array(); while ($this->obj_stmt->fetch()) { // Manual clone method - nasty, but required because of all the binding references // to avoid each row being === the last row in the result set $obj_row = NULL !== $this->str_result_class ? new $this->str_result_class() : new \stdClass(); foreach ($arr_fields as $obj_field) { $obj_row->{$obj_field->name} = $obj_result->{$obj_field->name}; } $mix_data[] = $obj_row; } } $this->obj_stmt->free_result(); return $mix_data; }
/** * Free memory associated with the resultset * * @return void */ public function flush() { if ($this->result instanceof mysqli_stmt) { $this->result->free_result(); } $this->result = null; $this->col_info = null; // Sanity check before using the handle if (empty($this->dbh) || !$this->dbh instanceof mysqli) { return; } // Clear out any results from a multi-query while (mysqli_more_results($this->dbh)) { mysqli_next_result($this->dbh); } }
/** * @param \mysqli_stmt $resource * @param string $column * * @return mixed[] */ protected function fetchResource($resource, $column) { $result = []; $metadata = $resource->result_metadata(); $fields = $metadata->fetch_fields(); if (count($fields) == 0) { return []; } $variables = []; $data = []; foreach ($fields as $field) { $variables[] =& $data[$field->name]; } $resource->bind_result(...$variables); while ($resource->fetch()) { $clone = []; foreach ($data as $key => $value) { $clone[$key] = $value; } $result[] = $clone; } $resource->free_result(); $this->fixTypes($result, $fields, $column); return $result; }
/** * Closes the cursor, allowing the statement to be executed again. * * @return bool */ public function closeCursor() { if ($stmt = $this->_stmt) { $this->_stmt->free_result(); return $this->_stmt->reset(); } return false; }
/** * Closes the cursor, allowing the statement to be executed again. * * @return bool */ public function closeCursor() { if ($stmt = $this->_stmt) { $mysqli = $this->_adapter->getConnection(); while ($mysqli->next_result()) { } $this->_stmt->free_result(); return $this->_stmt->reset(); } return false; }
function executeAndFetch($className = self::DEFAULT_CLASS_NAME, $type = self::RESULT_OBJECT, array $iteratorMap = null) { try { $this->execute(); } catch (\Exception $ex) { throw $ex; } switch ($type) { case self::RESULT_OBJECT: $objectBuilder = $className != self::DEFAULT_CLASS_NAME ? new ObjectBuilderUtil($className) : null; return $this->fetchObject($className, $objectBuilder, $iteratorMap); case self::RESULT_ARRAY: return $this->fetchArray($iteratorMap); default: throw new \InvalidArgumentException('Invalid Return Type'); } $this->stmt->free_result(); if (!$this->reusable) { $this->stmt->close(); unset($this->stmt); } else { $this->stmt->reset(); } }
/** * This helper method takes care of prepared statements' "bind_result method * , when the number of variables to pass is unknown. * * @param mysqli_stmt $stmt Equal to the prepared statement object. * * @return array The results of the SQL fetch. */ protected function _dynamicBindResults(mysqli_stmt $stmt) { $parameters = array(); $results = array(); // See http://php.net/manual/en/mysqli-result.fetch-fields.php $mysqlLongType = 252; $shouldStoreResult = false; $meta = $stmt->result_metadata(); // if $meta is false yet sqlstate is true, there's no sql error but the query is // most likely an update/insert/delete which doesn't produce any results if (!$meta && $stmt->sqlstate) { return array(); } $row = array(); while ($field = $meta->fetch_field()) { if ($field->type == $mysqlLongType) { $shouldStoreResult = true; } if ($this->_nestJoin && $field->table != $this->_tableName) { $field->table = substr($field->table, strlen(self::$prefix)); $row[$field->table][$field->name] = null; $parameters[] =& $row[$field->table][$field->name]; } else { $row[$field->name] = null; $parameters[] =& $row[$field->name]; } } // avoid out of memory bug in php 5.2 and 5.3. Mysqli allocates lot of memory for long* // and blob* types. So to avoid out of memory issues store_result is used // https://github.com/joshcam/PHP-MySQLi-Database-Class/pull/119 if ($shouldStoreResult) { $stmt->store_result(); } call_user_func_array(array($stmt, 'bind_result'), $parameters); $this->totalCount = 0; $this->count = 0; while ($stmt->fetch()) { if ($this->returnType == 'Object') { $x = new stdClass(); foreach ($row as $key => $val) { if (is_array($val)) { $x->{$key} = new stdClass(); foreach ($val as $k => $v) { $x->{$key}->{$k} = $v; } } else { $x->{$key} = $val; } } } else { $x = array(); foreach ($row as $key => $val) { $x[$key] = $val; } } $this->count++; array_push($results, $x); } if ($shouldStoreResult) { $stmt->free_result(); } $stmt->close(); // stored procedures sometimes can return more then 1 resultset if ($this->mysqli()->more_results()) { $this->mysqli()->next_result(); } if (in_array('SQL_CALC_FOUND_ROWS', $this->_queryOptions)) { $stmt = $this->mysqli()->query('SELECT FOUND_ROWS()'); $totalCount = $stmt->fetch_row(); $this->totalCount = $totalCount[0]; } if ($this->returnType == 'Json') { return json_encode($results); } return $results; }
/** * {@inheritdoc} */ public function closeCursor() { $this->_stmt->free_result(); return true; }
private function _dynamicBindResults(mysqli_stmt $stmt) { $parameters = array(); $results = array(); $meta = $stmt->result_metadata(); if (!$meta && $stmt->sqlstate) { return array(); } $row = array(); while ($field = $meta->fetch_field()) { $row[$field->name] = null; $parameters[] =& $row[$field->name]; } $stmt->store_result(); call_user_func_array(array($stmt, 'bind_result'), $parameters); $this->count = 0; while ($stmt->fetch()) { $result = array(); foreach ($row as $key => $val) { $result[$key] = $val; } array_push($results, (object) $result); $this->count++; } $stmt->free_result(); return $results; }
function fetchAll() { if (!$this->DB) { return; } $data = $this->Statement->result_metadata(); $out = array(); $fields = array(); if (!$data) { return null; } $length = 0; while (null != ($field = mysqli_fetch_field($data))) { $fields[] =& $out[$field->name]; $length += $field->length; } call_user_func_array(array($this->Statement, "bind_result"), $fields); $output = array(); $count = 0; //FIXME: store_result is needed, but using it causes crash if ($length >= 1000000) { if (!$this->Statement->store_result()) { throw new \Exception("Store_Result error on MySQLi prepared statement : " . $this->Statement->get_warnings()); } } while ($this->Statement->fetch()) { foreach ($out as $k => $v) { $output[$count][$k] = $v; } $count++; } $this->Statement->free_result(); return $count == 0 ? null : $output; }