Overview

Namespaces

  • None
  • PHP

Classes

  • AbstractTest
  • AMysql
  • AMysql_Abstract
  • AMysql_Expr
  • AMysql_Iterator
  • AMysql_Profiler
  • AMysql_Select
  • AMysql_Statement
  • AMysql_TestCase
  • ExceptionTest
  • ExprTest
  • IteratorTest
  • SelectTest
  • StatementTest

Exceptions

  • AMysql_Exception
  • Overview
  • Namespace
  • Class
  • Tree

Class AMysql_Statement

The statement class belonging to the AMysql_Abstract class, where mysql queries are built and handled. Most methods here are chainable, and many common AMysql_Abstract methods return a new instance of this class. A simple use example:

$amysql = new AMysql($conn); try { $stmt = $amysql->query('SELECT * FROM cms_content'); while ($row = $stmt->fetchAssoc()) { ... } } catch (AMysql_Exception $e) { echo $e->getDetails(); }

Visit https://github.com/amcsi/amysql

AMysql_Statement implements IteratorAggregate, Countable

Direct known subclasses

AMysql_Select
License: License; http://www.opensource.org/licenses/mit-license.php
Author: Szerémi Attila
Located at AMysql/Statement.php
Methods summary
public
# __construct( AMysql_Abstract $amysql )

__construct

__construct

Parameters

$amysql
AMysql_Abstract
$amysql
public resource|Mysqli
# getLink( )

Fetches the mysql link. If it is not set, try to take it from the amysql object. Of still not set, try to connect and take it.

Fetches the mysql link. If it is not set, try to take it from the amysql object. Of still not set, try to connect and take it.

Returns

resource|Mysqli
$link The mysql resource or Mysqli object
public boolean
# isMysqli( )

Checks whether mysqli is being used (as opposed to mysql)

Checks whether mysqli is being used (as opposed to mysql)

Returns

boolean
public AMysql_Iterator
# getIterator( )

Fetches the iterator for iterating through the results of the statement with the set fetch mode (default is assoc). This method is automatically called due to this class being an IteratorAggregate, so all you need to do is foreach your $statement object.

Fetches the iterator for iterating through the results of the statement with the set fetch mode (default is assoc). This method is automatically called due to this class being an IteratorAggregate, so all you need to do is foreach your $statement object.

Returns

AMysql_Iterator

Implementation of

IteratorAggregate::getIterator()
public AMysql_Statement
# setFetchMode( mixed $fetchMode )

Sets the fetch mode for fetch() and fetchAll() Any extra parameters are passed on to the handler for that fetch type. For example you can pass the class name and parameters for fetchObject here.

Sets the fetch mode for fetch() and fetchAll() Any extra parameters are passed on to the handler for that fetch type. For example you can pass the class name and parameters for fetchObject here.

Parameters

$fetchMode
mixed
$fetchMode The fetch mode. Use the AMysql_Abstract constants.

Returns

AMysql_Statement
(chainable)
public AMysql_Statement
# execute( mixed $binds = array () )

Executes a prepared statement, optionally accepting binds for replacing placeholders.

Executes a prepared statement, optionally accepting binds for replacing placeholders.

Parameters

$binds
mixed
$binds (Optional) The binds for the placeholders. This library supports names and unnames placeholders. To use unnamed placeholders, use question marks (?) as placeholders. A bind's key should be the index of the question mark. If $binds is not an array, it is casted to one. To use named placeholders, the placeholders must start with a non-alphanumeric, non-128+ character. If the key starts with an alphanumeric or 128+ character, the placeholder that is searched to be replaced will be the key prepended by a colon (:). Here are examples for what keys will replace what placeholders for the value: :key: => :key: :key => :key key => :key key: => :key: !key => !key élet => :élet All values are escaped and are automatically surrounded by apostrophes if needed. Do NOT add apostrophes around the string values as encapsulating for a mysql string.

Returns

AMysql_Statement
(chainable)

See

AMysql_Abstract::escape()
public
# getSql( string $prepared = null )

The sql string built up by the different preparing methods (prepare, select, insert etc.) is returned, having the placeholders being replaced by their binded values. You can debug what the final SQL string would be by calling this method.

The sql string built up by the different preparing methods (prepare, select, insert etc.) is returned, having the placeholders being replaced by their binded values. You can debug what the final SQL string would be by calling this method.

Parameters

$prepared
string
$prepared (Optional) Use this prepared string instead of the one set.
public string
# quoteInto( string $prepared, mixed $binds )

Like AMysql_Statement::getSql(), but you must give the prepared statement, the binds, and AMysql_Statement::$beforeSql is ignored.

Like AMysql_Statement::getSql(), but you must give the prepared statement, the binds, and AMysql_Statement::$beforeSql is ignored.

Parameters

$prepared
string
$prepared
$binds
mixed
$binds $binds are automatically type casted to an array.

Returns

string

See

AMysql_Statement::getSql()
protected string
# _replaceCallback( array $match )

Replaced a named placeholder with its replacement escaped via AMysql_Abstract::escape()

Replaced a named placeholder with its replacement escaped via AMysql_Abstract::escape()

Parameters

$match
array
$match

Returns

string
The replacement
public AMysql_Statement
# prepare( string $sql )

Prepares an SQL string for binding and execution. Use of this method is not recommended externally. Use the AMysql class's prepare method instead which returns a new AMysql_Statement instance.

Prepares an SQL string for binding and execution. Use of this method is not recommended externally. Use the AMysql class's prepare method instead which returns a new AMysql_Statement instance.

Parameters

$sql
string
$sql The SQL string to prepare.

Returns

AMysql_Statement
(chainable)
public AMysql_Statement
# query( string $sql, array $binds = array () )

Prepares a statement and executes it with the given binds.

Prepares a statement and executes it with the given binds.

Parameters

$sql
string
$sql The SQL string to prepare.
$binds
array
$binds @see $this->execute()

Returns

AMysql_Statement
(chainable)

See

AMysql_Statement::prepare()
AMysql_Statement::execute()
protected AMysql_Statement
# _query( string $sql )

Performs the actual query on the database with the given SQL string, making no further modifications on it.

Performs the actual query on the database with the given SQL string, making no further modifications on it.

Parameters

$sql
string
$sql The SQL string.

Returns

AMysql_Statement
(chainable)

Throws

AMysql_Exception
on error
public AMysql_Statement
# freeResults( )

Frees the mysql result resource.

Frees the mysql result resource.

Returns

AMysql_Statement
(chainable)
public array
# fetchAll( )

Returns all the results with each row in the format of that specified by the fetch mode.

Returns all the results with each row in the format of that specified by the fetch mode.

Returns

array

See

AMysql_Statement::setFetchMode()
public mixed
# fetch( )

Returns one row in the format specified by the fetch mode.

Returns one row in the format specified by the fetch mode.

Returns

mixed
Usually array. Can also be FALSE if there are no more rows. If AMysql_Abstract::FETCH_OBJECT is the fetch mode, then an object would be returned.

See

AMysql_Statement::setFetchMode()
public array|false
# fetchAssoc( )

Fetches one row with column names as the keys.

Fetches one row with column names as the keys.

Returns

array|false
public <Associative
# fetchAllAssoc( integer|string|boolean $keyColumn = false )

Fetches all rows and returns them as an array of associative arrays. The outer array is numerically indexed by default, but can be indexed by a field value.

Fetches all rows and returns them as an array of associative arrays. The outer array is numerically indexed by default, but can be indexed by a field value.

Parameters

$keyColumn
integer|string|boolean
$keyColumn (Optional) If a string, the cell of the given field will be the key for its row, so the result will not be an array numerically indexed from 0 in order. This value can also be an integer, specifying the index of the field with the key.

Returns

<Associative
result array>[]
public array|false
# fetchRow( )

Fetches the next row with column names as numeric indices. Returns FALSE if there are no more rows.

Fetches the next row with column names as numeric indices. Returns FALSE if there are no more rows.

Returns

array|false
public array|false
# fetchNum( )

Alias of AMysql_Statement::fetchRow()

Alias of AMysql_Statement::fetchRow()

Returns

array|false

See

AMysql_Statement::fetchRow()
public array|false
# fetchArray( )

Fetches the next row with column names and their indexes as keys.

Fetches the next row with column names and their indexes as keys.

Returns

array|false
public string|integer|false
# result( integer $row = 0, integer|string $field = 0 )

Returns the result of the given row and field. A warning is issued if the result on the given row and column does not exist.

Returns the result of the given row and field. A warning is issued if the result on the given row and column does not exist.

Parameters

$row
integer
$row (Optional) The row number.
$field
integer|string
$field (Optional) The field number or name.

Returns

string|integer|false
public mixed
# resultDefault( mixed $default, integer $row = 0, integer $field = 0 )

Returns the result of the given row and field, or the given value if the row doesn't exist

Returns the result of the given row and field, or the given value if the row doesn't exist

Parameters

$default
mixed
$default The value to return if the field is not found.
$row
integer
$row (Optional) The row number.
$field
integer
$field (Optional) The field.

Returns

mixed
public mixed
# resultNull( integer $row = 0, integer $field = 0 )

Returns the result of the given row and field, or null if the row doesn't exist

Returns the result of the given row and field, or null if the row doesn't exist

Parameters

$row
integer
$row (Optional) The row number.
$field
integer
$field (Optional) The field.

Returns

mixed
public integer
# resultInt( integer $row = 0, integer $field = 0 )

Returns the result of the given row and field as an integer. 0, if that result doesn't exist.

Returns the result of the given row and field as an integer. 0, if that result doesn't exist.

Parameters

$row
integer
$row (Optional) The row number.
$field
integer
$field (Optional) The field.

Returns

integer
public array
# fetchPairs( mixed $keyColumn = 0, mixed $valueColumn = 1 )

Returns an array of scalar values, where the keys are the values of the key column specified, and the values are the values of the value column specified.

Returns an array of scalar values, where the keys are the values of the key column specified, and the values are the values of the value column specified.

Parameters

$keyColumn
mixed
$keyColumn (Optional) column number or string for the keys. Default: 0.
$valueColumn
mixed
$valueColumn (Optional) column number or string for the values. Default: 1.

Returns

array
public
# pairUp( mixed $keyColumn = 0, mixed $valueColumn = 1 )

Alias of AMysql_Statement::fetchPairs()

Alias of AMysql_Statement::fetchPairs()

public array
# fetchAllColumn( mixed $column = 0 )

Returns all values of a specified column as an array.

Returns all values of a specified column as an array.

Parameters

$column
mixed
$column (Optional) column number or string for the values. Default: 0.

Returns

array
public array
# fetchAllColumns( string|integer $keyColumn = false )

Fetches all rows and returns them as an array of columns containing an array of values. Works simalarly to fetchAllAssoc(), but with the resulting array transposed.

Fetches all rows and returns them as an array of columns containing an array of values. Works simalarly to fetchAllAssoc(), but with the resulting array transposed.

e.g. [ 'id' => ['1', '2'], 'val' => ['val1', 'val2'] ]

Parameters

$keyColumn
string|integer
$keyColumn When building an array of arrays (list of values for that column) if this value is given, the indexes of the inner array will be equal to the value of the column in the row equivalent of data. Typically you want to choose the primary key. e.g. if $keyColumn is 'id', the example changes to: [ 'id' => ['1' => '1', '2' => '2'], 'val' => [ '1' => 'val1', '2' => 'val2' ] ]

Returns

array
public object
# fetchObject( string $className = 'stdClass', array $params = array() )

Fetches the next row as an object.

Fetches the next row as an object.

Parameters

$className
string
$className (Optional) The class to use. Default is stdClass.
$params
array
$params (Optional) The params to pass to the object.

Returns

object
public integer
# affectedRows( )

Returns the number of affected rows.

Returns the number of affected rows.

Returns

integer
public integer|false
# numRows( )

Returns the number of rows selected, or FALSE on failure.

Returns the number of rows selected, or FALSE on failure.

Returns

integer|false
public
# throwException( )

Throws an AMysqlException for the last mysql error. For internal use (if even used).

Throws an AMysqlException for the last mysql error. For internal use (if even used).

Throws

AMysql_Exception
public string
# escapeIdentifier( string $columnName, string $as = null )

Escapes an identifier and puts it between identifier quotes.

Escapes an identifier and puts it between identifier quotes.

Parameters

$columnName
string
$identifier The identifier
$as
string
$qc The quote character. Default: `

Returns

string
The escaped identifier.
public AMysql_Statement
# appendPrepare( string $sql )

Appends a string to the prepared string.

Appends a string to the prepared string.

Parameters

$sql
string
$sql The string to append.

Returns

AMysql_Statement
(chainable)
public AMysql_Statement
# bindValue( mixed $key, mixed $val )

Binds a value to the sql string.

Binds a value to the sql string.

Parameters

$key
mixed
$key If an integer, then the given index question mark will be replaced. If a string, then then, if it starts with an alphanumberic or 128+ ascii character, then a colon plus the string given will be replaced, otherwise the given string literally will be replaced. Example: if the string is foo then :foo will be replaced. if the string is !foo then !foo will be replaced if the string is :foo: then :foo: will be replaced. Note: don't worry about keys that have a common beginning. If foo and fool are set, :fool will not be replaced with the value given for foo.
$val
mixed
$val Bind this value for replacing the mark defined by $key. The value is escaped depeding on its type, apostrophes included, so do not add apostrophes in your prepared sqls.

Returns

AMysql_Statement
(chainable)
public AMysql_Statement
# bindParam( mixed $key, mixed & $val )

The same as $this->bindValue(), except that $val is binded by reference, meaning its value is extracted on execute.

The same as $this->bindValue(), except that $val is binded by reference, meaning its value is extracted on execute.

Parameters

$key
mixed
$key @see AMysql_Statement::bindValue()
$val
mixed
&$val Like $val in AMysql_Statement::bindValue(), but by reference.

Returns

AMysql_Statement
(chainable)

See

AMysql_Statement::bindValue()
public AMysql_Statement
# setBinds( array $binds )

Sets the binds binding question marks or named binds to values.

Sets the binds binding question marks or named binds to values.

Parameters

$binds
array
$binds The binds.

Returns

AMysql_Statement
(chainable)
public AMysql_Statement
# addBinds( array $binds )

Merges an array of binds with the ones already set. Only use for named parameters!

Merges an array of binds with the ones already set. Only use for named parameters!

Parameters

$binds
array
$binds The binds.

Returns

AMysql_Statement
(chainable)
public string
# buildColumnsValues( array $data )

Builds a columns list with values as a string. Can be an array of column-value pairs (2D for one row), can be an array of array of key-value pairs (3D for multiple rows), or can be an array with column names as the keys, each value being an array of values (3D for multiple rows).

Builds a columns list with values as a string. Can be an array of column-value pairs (2D for one row), can be an array of array of key-value pairs (3D for multiple rows), or can be an array with column names as the keys, each value being an array of values (3D for multiple rows).

e.g. (col1, col2) VALUES ('col1val1', 'col1val2'), ('col2val1', 'col2val2')

Parameters

$data
array
$data @see $this->insertReplace()

Returns

string
public string
# buildSet( array $data )

Puts together a string that is to be placed after a SET statement. i.e. column1 = 'value', int_col = 3

Puts together a string that is to be placed after a SET statement. i.e. column1 = 'value', int_col = 3

Parameters

$data
array
$data Keys are column names, values are the values unescaped

Returns

string
public AMysql_Statement
# update( string $tableName, array $data, string $where )

Prepares a mysql UPDATE unexecuted. By execution, have the placeholders of the WHERE statement binded. It is rather recommended to use AMysql_Abstract::update() instead, which lets you also bind the values in one call and it returns the success of the query.

Prepares a mysql UPDATE unexecuted. By execution, have the placeholders of the WHERE statement binded. It is rather recommended to use AMysql_Abstract::update() instead, which lets you also bind the values in one call and it returns the success of the query.

Parameters

$tableName
string
$tableName The table name.
$data
array
$data The array of data changes. A one-dimensional array with keys as column names and values as their values.
$where
string
$where An SQL substring of the WHERE clause.

Returns

AMysql_Statement
(chainable)
public AMysql_Statement
# insertReplace( string $type, string $tableName, array $data )

Prepares a mysql INSERT or REPLACE unexecuted. After this, you should just call $this->execute(). It is rather recommended to use AMysql_Abstract::insert() instead, which returns the last inserted id already.

Prepares a mysql INSERT or REPLACE unexecuted. After this, you should just call $this->execute(). It is rather recommended to use AMysql_Abstract::insert() instead, which returns the last inserted id already.

Parameters

$type
string
$type "$type INTO..." (INSERT, INSERT IGNORE, REPLACE) etc.
$tableName
string
$tableName The table name.
$data
array
$data A one or two-dimensional array. 1D: an associative array of keys as column names and values as their values. This inserts one row. 2D numeric: A numeric array where each value is an associative array with column-value pairs. Each outer, numeric value represents a row of data. 2D associative: An associative array where the keys are the columns, the values are numerical arrays, where each value represents the value for the new row of that key.

Returns

AMysql_Statement
(chainable)
public AMysql_Statement
# insert( string $tableName, array $data )

Performs an INSERT.

Performs an INSERT.

Parameters

$tableName
string
$tableName
$data
array
$data

Returns

AMysql_Statement
(chainable)

See

$this->insertReplace
public AMysql_Statement
# replace( string $tableName, array $data )

Performs a REPLACE.

Performs a REPLACE.

Parameters

$tableName
string
$tableName
$data
array
$data

Returns

AMysql_Statement
(chainable)

See

$this->insertReplace
public AMysql_Statement
# delete( string $tableName, string $where )

Prepares a mysql DELETE unexecuted. By execution, have the placeholders of the WHERE statement binded. It is rather recommended to use AMysql_Abstract::delete() instead, which lets you also bind the values in one call and it returns the success of the query.

Prepares a mysql DELETE unexecuted. By execution, have the placeholders of the WHERE statement binded. It is rather recommended to use AMysql_Abstract::delete() instead, which lets you also bind the values in one call and it returns the success of the query.

Parameters

$tableName
string
$tableName The table name.
$where
string
$where An SQL substring of the WHERE clause.

Returns

AMysql_Statement
(chainable)

See

AMysql_Abstract::delete()
public integer|false
# insertId( )

Returns the last insert id

Returns the last insert id

Returns

integer|false
public
# __destruct( )

Free the results.

Free the results.

public
# __set( mixed $name, mixed $value )
public integer
# count( )

Returns the number of rows in the result.

Returns the number of rows in the result.

Returns

integer

Throws

LogicException
If the query was not a read query.

Implementation of

Countable::count()
public
# reportErrorException( ErrorException $ex )

Handle an ErrorException thrown with the help of $this->errorHandlerCallback. For now, just suppress the error. For internal use.

Handle an ErrorException thrown with the help of $this->errorHandlerCallback. For now, just suppress the error. For internal use.

Parameters

$ex
ErrorException
$ex
public
# errorHandlerCallback( integer $errno, string $errstr, string $errfile = null, integer $errline = null, array $errcontext = null )

Changes warnings into exceptions. For internal use. Mainly for handling warnings generated by mysql functions/methods.

Changes warnings into exceptions. For internal use. Mainly for handling warnings generated by mysql functions/methods.

Parameters

$errno
integer
$errno
$errstr
string
$errstr
$errfile
string
$errfile
$errline
integer
$errline
$errcontext
array
$errcontext

Throws

ErrorException
protected
# handleError( string $msg, integer $code, string $query )

handleError

handleError

Parameters

$msg
string
$msg
$code
integer
$code
$query
string
$query

Throws

AMysql_Exception
protected
# handleException( AMysql_Exception $ex )

handleException

handleException

Parameters

$ex
AMysql_Exception

Throws

AMysql_Exception
Constants summary
integer CODE_QUERY_NOT_SUCCESSFUL 120000
#
Properties summary
public mixed $amysql
#
public mixed $error
#
public mixed $errno
#
public mixed $result
#
public array $results array ()
#
public mixed $query
#
public mixed $affectedRows
#
public mixed $throwExceptions
#
public mixed $lastException null
#
public mixed $insertId
#
public mixed $link
#
public mixed $isMysqli
#
protected mixed $_fetchMode
#
protected array $_fetchModeExtraArgs array ()
#
public string $beforeSql ''
#
public string $prepared ''
#
public array $binds array()
#
protected boolean $_executed false
#
protected mixed $_replacements
#
public float $queryTime
#

The time in took in seconds with microseconds to perform the query. It is automatically filled only when $profileQueries is set to true.

The time in took in seconds with microseconds to perform the query. It is automatically filled only when $profileQueries is set to true.

API documentation generated by ApiGen 2.8.0