/** * Closes the cursor, allowing the statement to be executed again. * * @return bool */ public function closeCursor() { if (!$this->_stmt) { return false; } $this->_stmt->close(); return true; }
/** * Close the cursor and statement * * @return Boolean */ public function close() { if ($this->_stmt) { $ret = $this->_stmt->close(); unset($this->_stmt); return $ret; } return false; }
/** * 释放资源 * * @return void */ public function free() { if ($this->prepare instanceof \mysqli_stmt) { $this->prepare->close(); $this->prepare = null; } }
public function preparedStatementClose() { if ($this->stmt) { $this->stmt->close(); $this->stmt = null; } }
/** * 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; }
/** * executar * Recebe os dados, monta o bind_param e executa. * * @param array * @throws Exception */ protected function executar(array $dados) { /** @var array */ $params = $this->prepararDados($dados); /** Passa os paramentros ao bind_param */ if (count($dados) > 0) { if ($this->stmt) { call_user_func_array(array($this->stmt, 'bind_param'), $this->makeValuesReferenced($params)); } else { throw new Exception("Erro ao executar \"{$this->mysqli->error}\"", $this->mysqli->errno); } } /** Executa a consulta e verifica se ocorreu algum erro */ if (!$this->stmt->execute()) { throw new Exception("Erro ao executar: (" . $this->stmt->error . ") ", $this->stmt->errno); } /** Preenche o array de dados caso haja algum retorno */ $this->result = array(); $r = $this->stmt->get_result(); if ($r) { while ($row = $r->fetch_assoc()) { $this->result[] = $row; } } /** Fecha o stamtment e a conexao com o banco */ $this->stmt->close(); $this->mysqli->close(); }
/** * Closes the cursor and the statement. * * @return bool */ public function close() { if ($this->_stmt) { $r = $this->_stmt->close(); $this->_stmt = null; return $r; } return false; }
/** * Method to free up the memory used for the result set. * * @param mixed $cursor The optional result set cursor from which to fetch the row. * * @return void * * @since 1.0 */ protected function freeResult($cursor = null) { $this->executed = false; if ($cursor instanceof \mysqli_result) { $cursor->free_result(); } if ($this->prepared instanceof \mysqli_stmt) { $this->prepared->close(); $this->prepared = null; } }
/** * * 关闭 stmt result mysqli的函数,传入这三个东西就行 * @param mysqli,stmt,result */ static function close(mysqli $mysqli = null, mysqli_stmt $stmt = null, mysqli_result $result = null) { if ($result != null) { $result->free(); } if ($stmt != null) { $stmt->close(); } if ($mysqli != null) { $mysqli->close(); } }
/** * Clean up all vars */ private function _cleanup() { $this->_select = array(); $this->_target = ""; //$this->_query_string = ""; $this->_where = array(); $this->_bind_params = array(''); $this->_joins = array(); $this->_limit = array(); $this->_orderBy = array(); $this->_query->close(); }
/** * Carica un indirizzo eseguendo un prepared statement * @param mysqli_stmt $stmt * @return null */ public function caricaIndirizzoDaStmt(mysqli_stmt $stmt) { if (!$stmt->execute()) { error_log("[caricaIndirizzoDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['id'], $row['destinatario'], $row['via_num'], $row['citta'], $row['provincia'], $row['cap'], $row['telefono']); if (!$bind) { error_log("[caricaIndirizzoDaStmt] impossibile" . " effettuare il binding in output"); return null; } if (!$stmt->fetch()) { return null; } $stmt->close(); return self::creaIndirizzoDaArray($row); }
/** * @return array * @throws \Exception */ public function fetchAll() { $results = [$this->translator->trans('sorry', array(), 'messages'), $this->translator->trans('wrong_query_execution', array(), 'messages')]; do { if (isset($this->connection->field_count) && !$this->connection->field_count) { if (strlen($this->connection->error) > 0) { $error = $this->connection->error; if ($this->connection->errno === 2014) { // Repair all tables of the test database $this->logger->error('[CONNECTION] ' . $error); } else { $this->logger->info('[CONNECTION] ' . $error); } throw new \Exception($error); } else { $this->lastResults = self::RESULTS_NONE; $results[1] = $this->translator->trans('no_record', array(), 'messages'); if (!is_null($this->statement) && $this->statement instanceof \mysqli_stmt) { /** * @see http://stackoverflow.com/a/25377031/2820730 about using \mysqli and xdebug */ $this->statement->close(); unset($this->statement); } } } else { $queryResult = $this->lastResults; /** * @var \mysqli_result $queryResult */ if (is_object($queryResult) && $queryResult instanceof \mysqli_result) { $results = []; while ($result = $queryResult->fetch_array(MYSQLI_ASSOC)) { if (!is_null($result)) { $results[] = $result; } } } } $this->queryCount--; } while ($this->queryCount > 0); return $results; }
/** * Carica una lista di articoli eseguendo un prepared statement * @param mysqli_stmt $stmt * @return null */ public function &caricaArticoliDaStmt(mysqli_stmt $stmt) { $articoli = array(); if (!$stmt->execute()) { error_log("[caricaArticoliDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['id'], $row['size'], $row['qty'], $row['prezzo'], $row['pizza_id']); if (!$bind) { error_log("[caricaArticoliDaStmt] impossibile" . " effettuare il binding in output"); return null; } while ($stmt->fetch()) { $articoli[] = self::creaArticoloDaArray($row); } $stmt->close(); return $articoli; }
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(); } }
/** * Carica un docente eseguendo un prepared statement * @param mysqli_stmt $stmt * @return null */ private function caricaAdminDaStmt(mysqli_stmt $stmt) { if (!$stmt->execute()) { error_log("[caricaAdminDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['admin_id'], $row['admin_username'], $row['admin_password'], $row['admin_nome'], $row['admin_cognome'], $row['admin_via'], $row['admin_civico'], $row['admin_cap'], $row['admin_citta'], $row['admin_telefono']); if (!$bind) { error_log("[caricaAdminDaStmt] impossibile" . " effettuare il binding in output"); return null; } if (!$stmt->fetch()) { return null; } $stmt->close(); return self::creaAdminDaArray($row); }
<?php require_once "connect.inc"; $mysql = new my_mysqli($host, $user, $passwd, $db, $port, $socket); $stmt = new mysqli_stmt($mysql, "SELECT 'foo' FROM DUAL"); $stmt->execute(); $stmt->bind_result($foo); $stmt->fetch(); $stmt->close(); $mysql->close(); var_dump($foo);
public function &creaAlbumDaStmt(mysqli_stmt $stmt) { $album = array(); if (!$stmt->execute()) { error_log("[creaAlbumDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['id'], $row['nome'], $row['autore'], $row['prezzo']); if (!$bind) { error_log("[creaAlbumDaStmt] impossibile" . " effettuare il binding in output"); return null; } while ($stmt->fetch()) { $album = self::creaAlbumDaArray($row); } $stmt->close(); return $album; }
/** * 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; }
/** * Releases the cursor. Should always be call after having fetched rows from * a query execution. * * @return void * @api */ public function free() { $this->statement->close(); }
/** * Close any open statements on destruction */ public function __destruct() { if ($this->obj_stmt) { $this->obj_stmt->close(); } }
public function __destruct() { $this->statement->close(); $this->closed = true; $this->currentRecord = false; }
/** * Close connection to database * * @param mysqli $mysqli * @param mysqli_stmt $stmt */ function closeConnection($mysqli, $stmt) { $mysqli->close(); $stmt->close(); }
/** Takes a prepared statement and fetches all objects from it * @param string $className Name of the class contained in table * @return array of objects */ private function RunAndFetchObjects($className, mysqli_stmt $stmt) { $result = $stmt->execute(); $ret = array(); $result = $stmt->get_result(); while ($object = $result->fetch_object()) { //NOTE! requires that we have a pk in the object not that obvious $ret[] = $object; //$ret[$object -> uid] = $object; } $stmt->close(); return $ret; }
/** * Carica una lista di pagamenti eseguendo un prepared statement * @param mysqli_stmt $stmt * @param $flag : 1 -> un metodo | 2 -> piu' di uno * @return null */ public function caricaPagamentiDaStmt(mysqli_stmt $stmt, $flag = 1) { if (!$stmt->execute()) { error_log("[caricaPagamentiDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['id'], $row['saldo'], $row['num_carta'], $row['cod_carta'], $row['scadenza_carta'], $row['titolare_carta'], $row['tipo_carta']); if (!$bind) { error_log("[caricaPagamentiDaStmt] impossibile" . " effettuare il binding in output"); return null; } if ($flag == 1) { if (!$stmt->fetch()) { return null; } $stmt->close(); return self::creaPagamentoDaArray($row); } else { $pagamenti = array(); while ($stmt->fetch()) { $pagamenti[] = self::creaPagamentoDaArray($row); } $stmt->close(); return $pagamenti; } }
/** * @param \mysqli_stmt $result */ public function closeResult(&$result) { if ($result instanceof \mysqli_stmt) { $result->close(); unset($result); } }
/** * Closes the cursor, allowing the statement to be executed again. * * @return void */ public function closeCursor() { $this->_stmt->close(); }
/** * @param mysqli $db * @param mysqli_stmt $stmt * @param string $error * @param int $code * @param string $status */ function stmtError($db, $stmt, $error = "Database error", $code = 500, $status = "Server error") { $dberror = $stmt->error; $stmt->close(); $db->rollback(); $db->close(); handleError($error . ': ' . $dberror); }
public function &caricaOrdiniDaStmt(mysqli_stmt $stmt) { $ordini = array(); if (!$stmt->execute()) { error_log("[caricaOrdiniDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['ordine_id'], $row['ordine_domicilio'], $row['ordine_prezzo'], $row['ordine_stato'], $row['ordine_data'], $row['cliente_id'], $row['admin_id']); if (!$bind) { error_log("[caricaOrdiniDaStmt] impossibile" . " effettuare il binding in output"); return null; } while ($stmt->fetch()) { $ordini[] = self::creaOrdineDaArray($row); } $stmt->close(); return $ordini; }
/** * Carica un cliente eseguendo un prepared statement * @param mysqli_stmt $stmt * @return null */ private function caricaClienteDaStmt(mysqli_stmt $stmt) { if (!$stmt->execute()) { error_log("[caricaClienteDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['id'], $row['username'], $row['password'], $row['email'], $row['nome'], $row['cognome'], $row['indirizzo']); if (!$bind) { error_log("[caricaClienteDaStmt] impossibile" . " effettuare il binding in output"); return null; } if (!$stmt->fetch()) { return null; } $stmt->close(); return self::creaClienteDaArray($row); }
/** * Carica uno studente eseguendo un prepared statement * @param mysqli_stmt $stmt * @return null */ private function caricaStudenteDaStmt(mysqli_stmt $stmt) { if (!$stmt->execute()) { error_log("[caricaStudenteDaStmt] impossibile" . " eseguire lo statement"); return null; } $row = array(); $bind = $stmt->bind_result($row['studenti_id'], $row['studenti_nome'], $row['studenti_cognome'], $row['studenti_matricola'], $row['studenti_email'], $row['studenti_citta'], $row['studenti_via'], $row['studenti_cap'], $row['studenti_provincia'], $row['studenti_numero_civico'], $row['studenti_username'], $row['studenti_password'], $row['CdL_id'], $row['CdL_nome'], $row['CdL_codice'], $row['dipartimenti_id'], $row['dipartimenti_nome']); if (!$bind) { error_log("[caricaStudenteDaStmt] impossibile" . " effettuare il binding in output"); return null; } if (!$stmt->fetch()) { return null; } $stmt->close(); return self::creaStudenteDaArray($row); }