Beispiel #1
0
 /**
  * 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;
 }
Beispiel #2
0
 /**
  * Get one row of data
  *
  * @return array|null
  * @access protected
  */
 protected function getOneRow()
 {
     if (null === $this->cols) {
         $result = $this->statement->result_metadata();
         if (false === $result) {
             return false;
         }
         $this->cols = [];
         // set column name
         foreach ($result->fetch_fields() as $col) {
             $this->cols[] = $col->name;
         }
         // bind values
         $this->vals = array_fill(0, count($this->cols), null);
         $refs = [];
         foreach ($this->vals as $i => &$f) {
             $refs[$i] =& $f;
         }
         call_user_func_array([$this->statement, 'bind_result'], $refs);
     }
     if ($this->statement->fetch()) {
         $row = [];
         foreach ($this->cols as $i => $col) {
             $row[$col] = $this->vals[$i];
         }
         return $row;
     }
     return false;
 }
 /**
  * @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;
 }
 /**
  * Mysqli's binding and returning of statement values
  * Mysqli requires you to bind variables to the extension in order to
  * get data out.  These values have to be references:
  *
  * @see http://php.net/manual/en/mysqli-stmt.bind-result.php
  * @throws Exception\RuntimeException
  * @return bool
  */
 protected function loadDataFromMysqliStatement()
 {
     $data = null;
     // build the default reference based bind structure, if it does not already exist
     if ($this->statementBindValues['keys'] === null) {
         $this->statementBindValues['keys'] = array();
         /* @var $resultResource \mysqli_result */
         $resultResource = $this->resource->result_metadata();
         foreach ($resultResource->fetch_fields() as $col) {
             $this->statementBindValues['keys'][] = $col->name;
         }
         $this->statementBindValues['values'] = array_fill(0, count($this->statementBindValues['keys']), null);
         $refs = array();
         foreach ($this->statementBindValues['values'] as $i => &$f) {
             $refs[$i] =& $f;
         }
         call_user_func_array(array($this->resource, 'bind_result'), $this->statementBindValues['values']);
     }
     if (($r = $this->resource->fetch()) === null) {
         if (!$this->isBuffered) {
             $this->resource->close();
         }
         return false;
     } elseif ($r === false) {
         throw new Exception\RuntimeException($this->resource->error);
     }
     // dereference
     for ($i = 0, $count = count($this->statementBindValues['keys']); $i < $count; $i++) {
         $this->currentData[$this->statementBindValues['keys'][$i]] = $this->statementBindValues['values'][$i];
     }
     $this->currentComplete = true;
     $this->nextComplete = true;
     $this->position++;
     return true;
 }
Beispiel #5
0
 /**
  * Executes a prepared statement.
  *
  * @param array $params OPTIONAL Values to bind to parameter placeholders.
  * @return bool
  * @throws Zend_Db_Statement_Mysqli_Exception
  */
 public function _execute(array $params = null)
 {
     if (!$this->_stmt) {
         return false;
     }
     // if no params were given as an argument to execute(),
     // then default to the _bindParam array
     if ($params === null) {
         $params = $this->_bindParam;
     }
     // send $params as input parameters to the statement
     if ($params) {
         array_unshift($params, str_repeat('s', count($params)));
         call_user_func_array(array($this->_stmt, 'bind_param'), $params);
     }
     // execute the statement
     $retval = $this->_stmt->execute();
     if ($retval === false) {
         /**
          * @see Zend_Db_Statement_Mysqli_Exception
          */
         require_once 'Zend/Db/Statement/Mysqli/Exception.php';
         throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement execute error : " . $this->_stmt->error);
     }
     // retain metadata
     if ($this->_meta === null) {
         $this->_meta = $this->_stmt->result_metadata();
         if ($this->_stmt->errno) {
             /**
              * @see Zend_Db_Statement_Mysqli_Exception
              */
             require_once 'Zend/Db/Statement/Mysqli/Exception.php';
             throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error: " . $this->_stmt->error);
         }
     }
     // statements that have no result set do not return metadata
     if ($this->_meta !== false) {
         // get the column names that will result
         $this->_keys = array();
         foreach ($this->_meta->fetch_fields() as $col) {
             $this->_keys[] = $this->_adapter->foldCase($col->name);
         }
         // set up a binding space for result variables
         $this->_values = array_fill(0, count($this->_keys), null);
         // set up references to the result binding space.
         // just passing $this->_values in the call_user_func_array()
         // below won't work, you need references.
         $refs = array();
         foreach ($this->_values as $i => &$f) {
             $refs[$i] =& $f;
         }
         $this->_stmt->store_result();
         // bind to the result variables
         call_user_func_array(array($this->_stmt, 'bind_result'), $this->_values);
     }
     return $retval;
 }
Beispiel #6
0
 /**
  * Executes a prepared statement.
  *
  * @param array $params OPTIONAL values to supply as input to statement parameters
  * @return void
  */
 public function execute(array $params = array())
 {
     // prepare for mysqli
     $sql = $this->_joinSql();
     $mysqli = $this->_connection->getConnection();
     $this->_stmt = $mysqli->prepare($sql);
     if ($this->_stmt === false || $mysqli->errno) {
         require_once 'Zend/Db/Statement/Mysqli/Exception.php';
         throw new Zend_Db_Statement_Mysqli_Exception("Mysqli prepare error: " . $mysqli->error);
     }
     // retain metadata
     $this->_meta = $this->_stmt->result_metadata();
     if ($this->_stmt->errno) {
         require_once 'Zend/Db/Statement/Mysqli/Exception.php';
         throw new Zend_Db_Statement_Mysqli_Exception("Mysqli statement metadata error for SQL = \"{$sql}\": " . $this->_stmt->error);
     }
     // statements that have no result set do not return metadata
     if ($this->_meta !== false) {
         // get the column names that will result
         $this->_keys = array();
         foreach ($this->_meta->fetch_fields() as $col) {
             $this->_keys[] = $col->name;
         }
         // set up a binding space for result variables
         $this->_values = array_fill(0, count($this->_keys), null);
         // set up references to the result binding space.
         // just passing $this->_values in the call_user_func_array()
         // below won't work, you need references.
         $refs = array();
         foreach ($this->_values as $i => &$f) {
             $refs[$i] =& $f;
         }
         // bind to the result variables
         call_user_func_array(array($this->_stmt, 'bind_result'), $this->_values);
     }
     // send $params as input parameters to the statement
     if ($params) {
         array_unshift($params, str_repeat('s', count($params)));
         call_user_func_array(array($this->_stmt, 'bind_param'), $params);
     }
     // execute the statement
     $this->_stmt->execute();
 }
 /**
  * {@inheritdoc}
  */
 public function execute($params = null)
 {
     if (null !== $this->_bindedValues) {
         if (null !== $params) {
             if (!$this->_bindValues($params)) {
                 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
             }
         } else {
             if (!call_user_func_array(array($this->_stmt, 'bind_param'), $this->_bindedValues)) {
                 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
             }
         }
     }
     if (!$this->_stmt->execute()) {
         throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
     }
     if (null === $this->_columnNames) {
         $meta = $this->_stmt->result_metadata();
         if (false !== $meta) {
             $columnNames = array();
             foreach ($meta->fetch_fields() as $col) {
                 $columnNames[] = $col->name;
             }
             $meta->free();
             $this->_columnNames = $columnNames;
             $this->_rowBindedValues = array_fill(0, count($columnNames), NULL);
             $refs = array();
             foreach ($this->_rowBindedValues as $key => &$value) {
                 $refs[$key] =& $value;
             }
             if (!call_user_func_array(array($this->_stmt, 'bind_result'), $refs)) {
                 throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
             }
         } else {
             $this->_columnNames = false;
         }
     }
     // We have a result.
     if (false !== $this->_columnNames) {
         $this->_stmt->store_result();
     }
     return true;
 }
 /**
  * (non-PHPdoc)
  * @see PreparedStatement::preparedStatementFetch()
  */
 public function preparedStatementFetch($msg = '')
 {
     if (!$this->stmt) {
         return false;
     }
     // first time, create an array of column names from the returned data set
     if (empty($this->preparedStatementResult)) {
         $this->resultFields = null;
         $this->preparedStatementResult = $this->stmt->result_metadata();
         if (is_object($this->preparedStatementResult)) {
             $this->resultFields = $this->preparedStatementResult->fetch_fields();
         } else {
             $this->preparedStatementResult = null;
             return false;
         }
         if (!empty($this->resultFields) && is_array($this->resultFields)) {
             $this->output_vars = $bound = array();
             foreach ($this->resultFields as $k => $field) {
                 $this->output_vars[$field->name] = null;
                 $bound[$k] =& $this->output_vars[$field->name];
             }
             call_user_func_array(array($this->stmt, "bind_result"), $bound);
         } else {
             $this->preparedStatementResult = null;
             return false;
         }
     }
     // Get the next results
     if ($this->stmt->fetch()) {
         $result = array();
         // FIXME: figure out how to avoid copying for each result
         // Right now copying is needed due to the fact that the bind_result
         // uses references so we can not give out the same array twice, as it will be
         // all referenced together and all have the data of the last row
         foreach ($this->output_vars as $k => $v) {
             $result[$k] = $v;
         }
         return $result;
     } else {
         return false;
     }
 }
Beispiel #9
0
 /**
  * Gets the results of the query
  */
 private function _getResults()
 {
     $meta = $this->_query->result_metadata();
     $parameters = array();
     $results = array();
     $row = array();
     while ($field = $meta->fetch_field()) {
         $row[$field->name] = null;
         $parameters[] =& $row[$field->name];
     }
     if (version_compare(phpversion(), '5.4', '<')) {
         $this->_query->store_result();
     }
     call_user_func_array(array($this->_query, 'bind_result'), $parameters);
     while ($this->_query->fetch()) {
         $x = array();
         foreach ($row as $key => $val) {
             $x[$key] = $val;
         }
         array_push($results, $x);
     }
     $this->_last_result = $results;
 }
Beispiel #10
0
 protected 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];
     }
     if (version_compare(phpversion(), '5.4', '<')) {
         $stmt->store_result();
     }
     call_user_func_array(array($stmt, 'bind_result'), $parameters);
     $this->totalCount = 0;
     $this->count = 0;
     while ($stmt->fetch()) {
         $x = array();
         foreach ($row as $key => $val) {
             $x[$key] = $val;
         }
         $this->count++;
         array_push($results, $x);
     }
     if ($this->_mysqli->more_results()) {
         $this->_mysqli->next_result();
     }
     if ($this->fetchTotalCount === true) {
         $this->fetchTotalCount = false;
         $stmt = $this->_mysqli->query('SELECT FOUND_ROWS();');
         $totalCount = $stmt->fetch_row();
         $this->totalCount = $totalCount[0];
     }
     return $results;
 }
Beispiel #11
0
 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;
 }
 /**
  * Executes the prepared statement. If the prepared statement included parameter
  * markers, you must either:
  * <ul>
  * <li>call {@link \TYPO3\CMS\Core\Database\PreparedStatement::bindParam()} to bind PHP variables
  * to the parameter markers: bound variables pass their value as input</li>
  * <li>or pass an array of input-only parameter values</li>
  * </ul>
  *
  * $input_parameters behave as in {@link \TYPO3\CMS\Core\Database\PreparedStatement::bindParams()}
  * and work for both named parameters and question mark parameters.
  *
  * Example 1:
  * <code>
  * $statement = $GLOBALS['TYPO3_DB']->prepare_SELECTquery('*', 'bugs', 'reported_by = ? AND bug_status = ?');
  * $statement->execute(array('goofy', 'FIXED'));
  * </code>
  *
  * Example 2:
  * <code>
  * $statement = $GLOBALS['TYPO3_DB']->prepare_SELECTquery('*', 'bugs', 'reported_by = :nickname AND bug_status = :status');
  * $statement->execute(array(':nickname' => 'goofy', ':status' => 'FIXED'));
  * </code>
  *
  * @param array $input_parameters An array of values with as many elements as there are bound parameters in the SQL statement being executed. The PHP type of each array value will be used to decide which PARAM_* type to use (int, string, boolean, NULL), so make sure your variables are properly casted, if needed.
  * @return bool Returns TRUE on success or FALSE on failure.
  * @throws \InvalidArgumentException
  * @api
  */
 public function execute(array $input_parameters = array())
 {
     $parameterValues = $this->parameters;
     if (!empty($input_parameters)) {
         $parameterValues = array();
         foreach ($input_parameters as $key => $value) {
             $parameterValues[$key] = array('value' => $value, 'type' => $this->guessValueType($value));
         }
     }
     if ($this->statement !== NULL) {
         // The statement has already been executed, we try to reset it
         // for current run but will set it to NULL if it fails for some
         // reason, just as if it were the first run
         if (!@$this->statement->reset()) {
             $this->statement = NULL;
         }
     }
     if ($this->statement === NULL) {
         // The statement has never been executed so we prepare it and
         // store it for further reuse
         $query = $this->query;
         $precompiledQueryParts = $this->precompiledQueryParts;
         $this->convertNamedPlaceholdersToQuestionMarks($query, $parameterValues, $precompiledQueryParts);
         if (!empty($precompiledQueryParts)) {
             $query = implode('', $precompiledQueryParts['queryParts']);
         }
         $this->statement = $GLOBALS['TYPO3_DB']->prepare_PREPAREDquery($query, $precompiledQueryParts);
         if ($this->statement === NULL) {
             return FALSE;
         }
     }
     $combinedTypes = '';
     $values = array();
     foreach ($parameterValues as $parameterValue) {
         switch ($parameterValue['type']) {
             case self::PARAM_NULL:
                 $type = 's';
                 $value = NULL;
                 break;
             case self::PARAM_INT:
                 $type = 'i';
                 $value = (int) $parameterValue['value'];
                 break;
             case self::PARAM_STR:
                 $type = 's';
                 $value = $parameterValue['value'];
                 break;
             case self::PARAM_BOOL:
                 $type = 'i';
                 $value = $parameterValue['value'] ? 1 : 0;
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf('Unknown type %s used for parameter %s.', $parameterValue['type'], $key), 1281859196);
         }
         $combinedTypes .= $type;
         $values[] = $value;
     }
     // ->bind_param requires second up to last arguments as references
     if (!empty($combinedTypes)) {
         $bindParamArguments = array();
         $bindParamArguments[] = $combinedTypes;
         $numberOfExtraParamArguments = count($values);
         for ($i = 0; $i < $numberOfExtraParamArguments; $i++) {
             $bindParamArguments[] =& $values[$i];
         }
         call_user_func_array(array($this->statement, 'bind_param'), $bindParamArguments);
     }
     $success = $this->statement->execute();
     // Store result
     if (!$success || $this->statement->store_result() === FALSE) {
         return FALSE;
     }
     if (empty($this->fields)) {
         // Store the list of fields
         if ($this->statement instanceof \mysqli_stmt) {
             $result = $this->statement->result_metadata();
             if ($result instanceof \mysqli_result) {
                 $fields = $result->fetch_fields();
                 $result->close();
             }
         } else {
             $fields = $this->statement->fetch_fields();
         }
         if (is_array($fields)) {
             foreach ($fields as $field) {
                 $this->fields[] = $field->name;
             }
         }
     }
     // New result set available
     $this->buffer = NULL;
     // Empty binding parameters
     $this->parameters = array();
     // Return the success flag
     return $success;
 }
Beispiel #13
0
 /**
  * Fetches the results from the MySQLi statement that was prepared and
  * executed. Will build an associative array containing all of the relevant
  * data from the query/statement that was executed.
  *
  * @param mysqli_stmt $statement The MySQLi statement to fetch the results
  *  from.
  * @return array Container for all information the statement stored after
  *  executing
  */
 protected static function _fetch_results($statement)
 {
     $params = array();
     $results = array();
     $metadata = $statement->result_metadata();
     while ($field = $metadata->fetch_field()) {
         $params[] =& $row[$field->name];
     }
     call_user_func_array(array($statement, 'bind_result'), $params);
     while ($statement->fetch()) {
         $tmp = array();
         foreach ($row as $key => $value) {
             if (is_string($value)) {
                 $value = stripslashes($value);
             }
             $tmp[$key] = $value;
         }
         $results[] = $tmp;
     }
     return $results;
 }
 /**
  * @param \mysqli_stmt $stmt
  * @param array        $out
  */
 public static function bindAssoc($stmt, &$out)
 {
     $data = $stmt->result_metadata();
     if (!$data) {
         self::mySqlError('mysqli_stmt::result_metadata');
     }
     $fields = [];
     $out = [];
     while ($field = $data->fetch_field()) {
         $fields[] =& $out[$field->name];
     }
     $b = call_user_func_array([$stmt, 'bind_result'], $fields);
     if ($b === false) {
         self::mySqlError('mysqli_stmt::bind_result');
     }
     $data->free();
 }
Beispiel #15
0
 /**
  * Import table headers and data from \mysqli_stmt
  *
  * @param \mysqli_stmt $stmt
  */
 public function importFromMysqli(\mysqli_stmt $stmt)
 {
     $meta = $stmt->result_metadata();
     $this->_header = array();
     while (($column = $field = $meta->fetch_field()) !== false) {
         $this->_header[] = $column->name;
     }
     $result = $stmt->get_result();
     $this->_data = array();
     while ($row = $result->fetch_array(MYSQLI_NUM)) {
         $this->_data[] = $row;
     }
     // if options is empty we want to regenerate defaults
     if (count($this->_options) < 1) {
         $this->setOptions();
     }
     $this->_executeFormats();
 }
 /**
  * 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;
 }
Beispiel #17
0
 /**
  * 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();
     $meta = $stmt->result_metadata();
     $row = array();
     while ($field = $meta->fetch_field()) {
         $row[$field->name] = null;
         $parameters[] =& $row[$field->name];
     }
     call_user_func_array(array($stmt, 'bind_result'), $parameters);
     while ($stmt->fetch()) {
         $x = array();
         foreach ($row as $key => $val) {
             $x[$key] = $val;
         }
         array_push($results, $x);
     }
     return $results;
 }
 /**
  * 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();
     $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()) {
         $row[$field->name] = null;
         $parameters[] =& $row[$field->name];
     }
     // avoid out of memory bug in php 5.2 and 5.3
     // https://github.com/joshcam/PHP-MySQLi-Database-Class/pull/119
     if (version_compare(phpversion(), '5.4', '<')) {
         $stmt->store_result();
     }
     call_user_func_array(array($stmt, 'bind_result'), $parameters);
     while ($stmt->fetch()) {
         $x = array();
         foreach ($row as $key => $val) {
             $x[$key] = $val;
         }
         $this->count++;
         array_push($results, $x);
     }
     return $results;
 }
 /**
  * Dynamically binds the result of the supplied prepared statement to a 2d array, where each element in the array is another array
  * representing a database row.
  *
  * @param mysqli_stmt $stmt
  *
  * @return array A 2D array containing the query result.
  *
  * @since 1.1
  */
 private function bindResult($stmt)
 {
     $result = array();
     $metadata = $stmt->result_metadata();
     $fields = $metadata->fetch_fields();
     while (true) {
         $pointers = array();
         $row = array();
         $pointers[] = $stmt;
         foreach ($fields as $field) {
             $fieldname = $field->name;
             $pointers[] =& $row[$fieldname];
         }
         call_user_func_array('mysqli_stmt_bind_result', $pointers);
         if (!$stmt->fetch()) {
             break;
         }
         $result[] = $row;
     }
     $metadata->free();
     return $result;
 }
 /**
  * Set the returned result object {mysqli_result} metadata from a prepared statement.
  *
  * @return DatabaseInterface The current instance
  */
 protected function setResultMetadata() : DatabaseInterface
 {
     $this->setProperty('resultMetadata', $this->stmt->result_metadata());
     return $this;
 }
Beispiel #21
0
 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;
 }
Beispiel #22
0
 /**
  * Returns results from a statement
  * @param \mysqli_stmt $stmt
  * @return array
  */
 private function getResultsFromStmt($stmt)
 {
     # Get metadata for field names
     $meta = $stmt->result_metadata();
     # Return no results
     if (!$meta) {
         return array();
     }
     # Dynamically create an array of variables to use to bind the results
     $fields = array();
     while ($field = $meta->fetch_field()) {
         $var = $field->name;
         ${$var} = null;
         $fields[$var] =& ${$var};
     }
     # Bind Results
     call_user_func_array(array($stmt, 'bind_result'), $fields);
     # Fetch Results
     $i = 0;
     $results = array();
     while ($stmt->fetch()) {
         $results[$i] = array();
         foreach ($fields as $k => $v) {
             $results[$i][$k] = $v;
         }
         $i++;
     }
     $meta->free();
     return $results;
 }
Beispiel #23
0
 protected static function getIntegerColumns(mysqli_stmt $stmt)
 {
     if (!$stmt->field_count) {
         return false;
     }
     $result = $stmt->result_metadata();
     $fieldInfo = mysqli_fetch_fields($result);
     $intFieldNames = array();
     for ($i = 0, $len = sizeOf($fieldInfo); $i < $len; $i++) {
         switch ($fieldInfo[$i]->type) {
             // From http://us2.php.net/manual/en/mysqli-result.fetch-field-direct.php
             case 1:
             case 2:
             case 3:
             case 8:
             case 9:
                 $intFieldNames[] = $fieldInfo[$i]->name;
                 break;
         }
     }
     return $intFieldNames;
 }
Beispiel #24
0
 /**
  * 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();
     $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()) {
         $row[$field->name] = null;
         $parameters[] =& $row[$field->name];
     }
     // avoid out of memory bug in php 5.2 and 5.3
     //if (version_compare (phpversion(), '5.4', '<'))
     // NOTE: Always store results, because it seems that memory bug in php 5.2 and 5.3 has re-surfaced
     $stmt->store_result();
     call_user_func_array(array($stmt, 'bind_result'), $parameters);
     $this->count = 0;
     while ($stmt->fetch()) {
         $x = array();
         foreach ($row as $key => $val) {
             $x[$key] = $val;
         }
         $this->count++;
         array_push($results, $x);
     }
     return $results;
 }
Beispiel #25
0
 /**
  * Fetches the result from a prepared query.
  *
  * @param \MYSQLi_STMT $query The prepared query to fetch the result from.
  *
  * @return string[] The result array or null in case we have no result.
  *
  * @author Christian Achatz
  * @version
  * Version 0.1, 09.03.2010<br />
  */
 private function fetchBindResult(\mysqli_stmt $query)
 {
     $result = null;
     do {
         $metaData = $query->result_metadata();
         // in case the meta data is not present (e.g. for INSERT statements),
         // we cannot fetch any data. thus we return null to indicate no result
         if ($metaData === false) {
             break;
         }
         $resultRow = [];
         $resultParams = [];
         while ($field = $metaData->fetch_field()) {
             $resultParams[] =& $resultRow[$field->name];
         }
         $bindResult = [];
         call_user_func_array([&$query, 'bind_result'], $resultParams);
         while ($query->fetch()) {
             $currentRow = [];
             foreach ($resultRow as $key => $val) {
                 $currentRow[$key] = $val;
             }
             $bindResult[] = $currentRow;
         }
         $result[] = $bindResult;
     } while ($query->more_results() && $query->next_result());
     // for sprocs
     return count($result) === 1 ? $result[0] : $result;
 }
 /**
  * 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();
     $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()) {
         $row[$field->name] = null;
         $parameters[] =& $row[$field->name];
     }
     $stmt->store_result();
     call_user_func_array(array($stmt, 'bind_result'), $parameters);
     while ($stmt->fetch()) {
         $x = array();
         foreach ($row as $key => $val) {
             $x[$key] = $val;
         }
         $this->count++;
         array_push($results, $x);
     }
     return $results;
 }
Beispiel #27
0
 /**
  * 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();
     $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()) {
         $row[$field->name] = null;
         $parameters[] =& $row[$field->name];
     }
     // avoid out of memory bug in php 5.2 and 5.3
     // https://github.com/joshcam/PHP-MySQLi-Database-Class/pull/119
     if (version_compare(phpversion(), '5.4', '<')) {
         $stmt->store_result();
     }
     call_user_func_array(array($stmt, 'bind_result'), $parameters);
     $this->totalCount = 0;
     $this->count = 0;
     while ($stmt->fetch()) {
         $x = array();
         foreach ($row as $key => $val) {
             $x[$key] = $val;
         }
         ++$this->count;
         array_push($results, $x);
     }
     // 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];
     }
     return $results;
 }