Fr3nch13/CakePHP Utilities

StatementInterface

Represents a database statement. Concrete implementations can either use PDOStatement or a native driver

Table of Contents

Constants

FETCH_TYPE_ASSOC  = 'assoc'
Used to designate that an associated array be returned in a result when calling fetch methods
FETCH_TYPE_NUM  = 'num'
Used to designate that numeric indexes be returned in a result when calling fetch methods
FETCH_TYPE_OBJ  = 'obj'
Used to designate that a stdClass object be returned in a result when calling fetch methods

Methods

bind()  : void
Binds a set of values to statement object with corresponding type
bindValue()  : void
Assign a value to a positional or named variable in prepared query. If using positional variables you need to start with index one, if using named params then just use the name in any order.
closeCursor()  : void
Closes a cursor in the database, freeing up any resources and memory allocated to it. In most cases you don't need to call this method, as it is automatically called after fetching all results from the result set.
columnCount()  : int
Returns the number of columns this statement's results will contain
count()  : int
Statements can be passed as argument for count() to return the number for affected rows from last execution
errorCode()  : string|int
Returns the error code for the last error that occurred when executing this statement
errorInfo()  : array<string|int, mixed>
Returns the error information for the last error that occurred when executing this statement
execute()  : bool
Executes the statement by sending the SQL query to the database. It can optionally take an array or arguments to be bound to the query variables. Please note that binding parameters from this method will not perform any custom type conversion as it would normally happen when calling `bindValue`
fetch()  : mixed
Returns the next row for the result set after executing this statement.
fetchAll()  : array<string|int, mixed>|false
Returns an array with all rows resulting from executing this statement
fetchColumn()  : mixed
Returns the value of the result at position.
lastInsertId()  : string|int
Returns the latest primary inserted using this statement
rowCount()  : int
Returns the number of rows affected by this SQL statement

Constants

FETCH_TYPE_ASSOC

Used to designate that an associated array be returned in a result when calling fetch methods

public string FETCH_TYPE_ASSOC = 'assoc'

FETCH_TYPE_NUM

Used to designate that numeric indexes be returned in a result when calling fetch methods

public string FETCH_TYPE_NUM = 'num'

FETCH_TYPE_OBJ

Used to designate that a stdClass object be returned in a result when calling fetch methods

public string FETCH_TYPE_OBJ = 'obj'

Methods

bind()

Binds a set of values to statement object with corresponding type

public bind(array<string|int, mixed> $params, array<string|int, mixed> $types) : void
Parameters
$params : array<string|int, mixed>

list of values to be bound

$types : array<string|int, mixed>

list of types to be used, keys should match those in $params

bindValue()

Assign a value to a positional or named variable in prepared query. If using positional variables you need to start with index one, if using named params then just use the name in any order.

public bindValue(string|int $column, mixed $value[, string|int|null $type = 'string' ]) : void

It is not allowed to combine positional and named variables in the same statement

Examples:

$statement->bindValue(1, 'a title');
$statement->bindValue('active', true, 'boolean');
$statement->bindValue(5, new \DateTime(), 'date');
Parameters
$column : string|int

name or param position to be bound

$value : mixed

The value to bind to variable in query

$type : string|int|null = 'string'

name of configured Type class, or PDO type constant.

closeCursor()

Closes a cursor in the database, freeing up any resources and memory allocated to it. In most cases you don't need to call this method, as it is automatically called after fetching all results from the result set.

public closeCursor() : void

columnCount()

Returns the number of columns this statement's results will contain

public columnCount() : int

Example:

 $statement = $connection->prepare('SELECT id, title from articles');
 $statement->execute();
 echo $statement->columnCount(); // outputs 2
Return values
int

count()

Statements can be passed as argument for count() to return the number for affected rows from last execution

public count() : int
Return values
int

errorCode()

Returns the error code for the last error that occurred when executing this statement

public errorCode() : string|int
Return values
string|int

errorInfo()

Returns the error information for the last error that occurred when executing this statement

public errorInfo() : array<string|int, mixed>
Return values
array<string|int, mixed>

execute()

Executes the statement by sending the SQL query to the database. It can optionally take an array or arguments to be bound to the query variables. Please note that binding parameters from this method will not perform any custom type conversion as it would normally happen when calling `bindValue`

public execute([array<string|int, mixed>|null $params = null ]) : bool
Parameters
$params : array<string|int, mixed>|null = null

list of values to be bound to query

Return values
bool

true on success, false otherwise

fetch()

Returns the next row for the result set after executing this statement.

public fetch([string|int $type = 'num' ]) : mixed

Rows can be fetched to contain columns as names or positions. If no rows are left in result set, this method will return false

Example:

 $statement = $connection->prepare('SELECT id, title from articles');
 $statement->execute();
 print_r($statement->fetch('assoc')); // will show ['id' => 1, 'title' => 'a title']
Parameters
$type : string|int = 'num'

'num' for positional columns, assoc for named columns, or PDO fetch mode constants.

Return values
mixed

Result array containing columns and values or false if no results are left

fetchAll()

Returns an array with all rows resulting from executing this statement

public fetchAll([string|int $type = 'num' ]) : array<string|int, mixed>|false

Example:

 $statement = $connection->prepare('SELECT id, title from articles');
 $statement->execute();
 print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
Parameters
$type : string|int = 'num'

num for fetching columns as positional keys or assoc for column names as keys

Return values
array<string|int, mixed>|false

list of all results from database for this statement or false on failure.

fetchColumn()

Returns the value of the result at position.

public fetchColumn(int $position) : mixed
Parameters
$position : int

The numeric position of the column to retrieve in the result

Return values
mixed

Returns the specific value of the column designated at $position

lastInsertId()

Returns the latest primary inserted using this statement

public lastInsertId([string|null $table = null ][, string|null $column = null ]) : string|int
Parameters
$table : string|null = null

table name or sequence to get last insert value from

$column : string|null = null

the name of the column representing the primary key

Return values
string|int

rowCount()

Returns the number of rows affected by this SQL statement

public rowCount() : int

Example:

 $statement = $connection->prepare('SELECT id, title from articles');
 $statement->execute();
 print_r($statement->rowCount()); // will show 1
Return values
int

        
On this page

Search results