Bluz\Db\Db::query PHP Method

query() public method

Example of usage $db->query("SET NAMES 'utf8'");
public query ( string $sql, array $params = [], array $types = [] ) : integer
$sql string SQL query with placeholders "UPDATE users SET name = :name WHERE id = :id"
$params array params for query placeholders (optional) array (':name' => 'John', ':id' => '123')
$types array Types of params (optional) array (':name' => \PDO::PARAM_STR, ':id' => \PDO::PARAM_INT)
return integer the number of rows
    public function query($sql, $params = [], $types = [])
    {
        $stmt = $this->handler()->prepare($sql);
        foreach ($params as $key => &$param) {
            $stmt->bindParam(is_int($key) ? $key + 1 : ":" . $key, $param, $types[$key] ?? \PDO::PARAM_STR);
        }
        $this->log($sql, $params);
        $stmt->execute($params);
        $this->ok();
        return $stmt->rowCount();
    }

Usage Example

Esempio n. 1
0
 /**
  * Transaction Fail
  */
 public function testTransactionFalse()
 {
     $result = $this->db->transaction(function () {
         $this->db->query('DELETE FROM test LIMIT 1');
         $this->db->query('DELETE FROM test LIMIT 1');
         $this->db->query('DELETE FROM test LIMIT 1');
         $this->db->query('DELETE FROM notexiststable LIMIT 1');
     });
     $this->assertFalse($result);
 }