/** * Execute a query * * @param array $bind_variables * * @throws \Exception * * @return Statement */ public function execute($bind_variables = array()) { if ($this->executed) { throw new \Exception('Statement::execute() called twice.'); } // Parameters may be either named (e.g. :foo) or positional (e.g. ?). // Named parameters may take any type. Positional parameters are always strings. // Queries should use one format or the other. foreach ($bind_variables as $key => $bind_variable) { if (is_numeric($key)) { // Positional parameters are numeric (starting at 1) $key = 1 + $key; } else { // Named parameters are prefixed with a colon $key = ':' . $key; } switch (gettype($bind_variable)) { case 'NULL': $this->pdo_statement->bindValue($key, $bind_variable, PDO::PARAM_NULL); break; case 'boolean': $this->pdo_statement->bindValue($key, (int) $bind_variable, PDO::PARAM_INT); break; case 'integer': $this->pdo_statement->bindValue($key, $bind_variable, PDO::PARAM_INT); break; default: $this->pdo_statement->bindValue($key, $bind_variable, PDO::PARAM_STR); break; } } $start = microtime(true); $this->pdo_statement->execute(); $end = microtime(true); // If it was a SELECT statement, we cannot run it again. $this->executed = strpos($this->pdo_statement->queryString, 'SELECT') === 0; Database::logQuery($this->pdo_statement->queryString, $this->pdo_statement->rowCount(), $end - $start, $bind_variables); return $this; }