DboSource::execute PHP Method

execute() public method

If Configure::read('debug') is set, the log is shown all the time, else it is only shown on errors. ### Options - log - Whether or not the query should be logged to the memory log.
public execute ( string $sql, array $options = [], array $params = [] ) : mixed
$sql string SQL statement
$options array The options for executing the query.
$params array values to be bound to the query.
return mixed Resource or object representing the result set, or false on failure
    public function execute($sql, $options = array(), $params = array())
    {
        $options += array('log' => $this->fullDebug);
        $t = microtime(true);
        $this->_result = $this->_execute($sql, $params);
        if ($options['log']) {
            $this->took = round((microtime(true) - $t) * 1000, 0);
            $this->numRows = $this->affected = $this->lastAffected();
            $this->logQuery($sql, $params);
        }
        return $this->_result;
    }

Usage Example

Example #1
0
 /**
  * Setup the admin user.
  *
  * @return bool
  */
 public function setupAdmin()
 {
     $answer = strtoupper($this->in('<question>Would you like to [c]reate a new user, or use an [e]xisting user?</question>', array('C', 'E')));
     $userMap = Configure::read('Forum.userMap');
     $statusMap = Configure::read('Forum.statusMap');
     // New User
     if ($answer === 'C') {
         $this->install['username'] = $this->_newUser('username');
         $this->install['password'] = $this->_newUser('password');
         $this->install['email'] = $this->_newUser('email');
         $result = $this->db->execute(sprintf("INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`) VALUES (%s, %s, %s, %s);", $this->install['table'], $userMap['username'], $userMap['password'], $userMap['email'], $userMap['status'], $this->db->value(Sanitize::clean($this->install['username'])), $this->db->value(Security::hash($this->install['password'], null, true)), $this->db->value($this->install['email']), $this->db->value($statusMap['active'])));
         if ($result) {
             $this->install['user_id'] = $this->db->lastInsertId();
         } else {
             $this->out('<error>An error has occurred while creating the user</error>');
             return $this->setupAdmin();
         }
         // Old User
     } else {
         if ($answer === 'E') {
             $this->install['user_id'] = $this->_oldUser();
             // Redo
         } else {
             return $this->setupAdmin();
         }
     }
     // Give ACL
     $result = ClassRegistry::init('Forum.Access')->add(array('parent_id' => $this->install['acl_admin'], 'foreign_key' => $this->install['user_id']));
     if (!$result) {
         $this->out('<error>An error occurred while granting administrator access</error>');
         return $this->setupAdmin();
     }
     return true;
 }
All Usage Examples Of DboSource::execute
DboSource