/** * Executes a query previously prepared using the prepare() method. * When executed any parameter markers which exist will automatically be replaced with the * parameters passed as argument. * * If the statement is UPDATE, DELETE, or INSERT, the total number of affected rows can be * determined by using the MyDBD_PreparedStatement::affectedRows() method. Likewise, if the query * yields a result set, the MyDBD_ResultSet::next() function is used. * * @see MyDBD_PreparedStatement::prepare() * @see MyDBD_PreparedStatement::affectedRows() * @see MyDBD_StatementResultSet::next() * * @param string|integer|double $param,... parameters to replace markers of the prepared query. * * Note: the type of the parameter is important, it will be used to determine the type of binding * to use for the parameter. $sth->execute('42') isn't equal to $sth->execute(42). * * @throws SQLMismatchException if the number of parameters passed isn't equal to the * number of markers in the prepared query. * @throws SQLNotPreparedStatementException if no statement have been prepared before calling this * method. * * @return MyDBD_StatementResultSet if the query yields a result set, NULL otherwise. */ public function execute() { $params = null; if (null === $this->preparedQuery) { throw new SQLNotPreparedStatementException('Cannot execute a not prepared statement.'); } if ($queryLog = $this->options['query_log']) { $start = microtime(true); } if ($this->stmt->param_count > 0) { $params = func_get_args(); // PDO compat: support params as an array in first argument if (count($params) === 1 && is_array($params[0])) { $params = $params[0]; } $this->bindParams($params); } $this->stmt->execute(); $this->stmt->store_result(); $this->handleErrors($this->preparedQuery, $params); if ($queryLog) { MyDBD_Logger::log('execute', $this->preparedQuery, isset($params) ? $params : null, microtime(true) - $start); } if ($metadata = $this->stmt->result_metadata()) { // integrity problem due to mysqli design limitation // you can't store several result set on several executed query // from the same prepared statment if (null === $this->resultSet) { $this->resultSet = new MyDBD_StatementResultSet($this->stmt, $metadata, $this->options); } return $this->resultSet->reset(); } else { return null; } }
/** * Performs a query on the database. * * @param string $query The SQL query. * @param mixed $param... Values for placeholders of the SQL query if any. Quantity of values passed * must match quantity of placeholders in the query. If placeholders are * are used in the query, the query will be prepared and this parameter * is mandatory, otherwise, this parameter doesn't have to be given. * NOTE: for backward compat, if only one param is given and is an array, * the content of the array is treated as param list. * * <code> * $dbh->query('SELECT COUNT(*) FROM table'); * * $dbh->query('INSERT INTO table (username, password) VALUES(?, ?)', $username, $password); * * # compatility mode * $dbh->query('INSERT INTO table (username, password) VALUES(?, ?)', array($username, $password)); * </code> * * @throws SQLException if an error happen * * @return mixed If no placeholder where used or MyDBD_StatementResultSet if placeholders * where used and query had to be prepared. If query does not generate result set, * boolean is returned. */ public function query() { $params = func_get_args(); $query = array_shift($params); // backward compat, support params given as array if (count($params) == 1 && is_array($params[0])) { $params = $params[0]; } $this->injectExtendedInfo($query); if ($this->options['readonly']) { $this->checkReadonlyQuery($query); } if ($this->options['query_log']) { $start = microtime(true); } if (count($params) > 0) { $sth = $this->options['query_prepare_cache'] ? $this->prepareCached($query) : $this->prepare($query); $result = $sth->execute($params); } else { $result = $this->link()->query($query); $this->handleErrors($query); $this->lastQueryHandle = $this->link(); // used by getAffectedRows() if ($result instanceof mysqli_result) { $result = new MyDBD_ResultSet($result, $this->options); } if ($this->options['query_log']) { MyDBD_Logger::log('query', $query, null, microtime(true) - $start); } } return $result; }