\Cake\DatabaseStatementInterface

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

Summary

Methods
Constants
bindValue()
closeCursor()
columnCount()
errorCode()
errorInfo()
execute()
fetch()
fetchAll()
rowCount()
count()
bind()
lastInsertId()
FETCH_TYPE_NUM
FETCH_TYPE_ASSOC
FETCH_TYPE_OBJ
No protected methods found
N/A
No private methods found
N/A

Constants

FETCH_TYPE_NUM

FETCH_TYPE_NUM = 'num' : string

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

FETCH_TYPE_ASSOC

FETCH_TYPE_ASSOC = 'assoc' : string

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

FETCH_TYPE_OBJ

FETCH_TYPE_OBJ = 'obj' : string

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

Methods

bindValue()

bindValue(string|integer  $column, mixed  $value, string  $type = 'string') : 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.

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

string|integer $column

name or param position to be bound

mixed $value

The value to bind to variable in query

string $type

name of configured Type class

closeCursor()

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()

columnCount() : integer

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

Example:

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

Returns

integer

errorCode()

errorCode() : integer|string

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

Returns

integer|string

errorInfo()

errorInfo() : array

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

Returns

array

execute()

execute(array|null  $params = null) : boolean

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`

Parameters

array|null $params

list of values to be bound to query

Returns

boolean —

true on success, false otherwise

fetch()

fetch(string  $type = 'num') : array|false

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

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

string $type

'num' for positional columns, assoc for named columns

Returns

array|false —

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

fetchAll()

fetchAll(string  $type = 'num') : array

Returns an array with all rows resulting from executing this statement

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

string $type

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

Returns

array —

list of all results from database for this statement

rowCount()

rowCount() : integer

Returns the number of rows affected by this SQL statement

Example:

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

Returns

integer

count()

count() : integer

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

Returns

integer

bind()

bind(array  $params, array  $types) : void

Binds a set of values to statement object with corresponding type

Parameters

array $params

list of values to be bound

array $types

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

lastInsertId()

lastInsertId(string|null  $table = null, string|null  $column = null) : string

Returns the latest primary inserted using this statement

Parameters

string|null $table

table name or sequence to get last insert value from

string|null $column

the name of the column representing the primary key

Returns

string