Methods summary
public
|
#
__construct( resource|mysqli |array|string $resOrArrayOrHost = null, mixed $username = null, mixed $password = null, mixed $newLink = false, mixed $clientFlags = 0 )
Parameters
- $resOrArrayOrHost
resource|mysqli |array|string $resOrArrayOrHost (Optional) Either a valid mysql or mysqli connection
resource/object, or a connection details array as according to setConnDetails()
(doesn't auto connect), or parameters you would normally pass to the
mysql_connect() function. (auto connects) (NOTE that this last construction
method is discouraged and may be deprecated and removed in later versions) (Also
note that I mention the arguments mysql_connect(), but only the arguments. This
library will still connect to mysqli if it is available)
- $username
- $password
- $newLink
- $clientFlags
See
$this->setConnDetails()
$this->setConnDetails()
Constructor
|
public
AMysql_Abstract
|
#
setConnDetails( array $cd )
Sets the connection details
Sets the connection details
Parameters
- $cd
array $connDetails An array of details: host - hostname or ip username - username
password - password db - db to auto connect to port - port driver - force
'mysql' or 'mysqli' socket - socket defaultAutoCommit - @see
$this->defaultAutoCommit autoPingSeconds - @see $this->autoPingSeconds
autoReconnect - @see $this->autoReconnect
Returns
|
public
AMysql_Abstract
|
#
oldSetConnDetails( string $host = null, string $username = null, string $password = null, boolean $newLink = false, integer $clientFlags = 0 )
Parameters
- $host
string $host (optional)
- $username
string $username (optional)
- $password
string $password (optional)
- $newLink
boolean $newLink (optional)
- $clientFlags
integer $clientFlags (optional)
Returns
See
mysql_connect()
|
public
AMysql_Abstract
|
#
connect( )
Connects to the database with the configured settings. Sets $this->link
and $this->isMysqli
Connects to the database with the configured settings. Sets $this->link
and $this->isMysqli
Returns
|
public
AMysql_Abstract
|
#
close( )
Closes the connection and unsets its link.
Closes the connection and unsets its link.
Returns
|
public
resource|mysqli
|
#
autoConnect( )
Returns the DB connection link. Connects to the DB if not connected. Does not
check if the connection has since been broken.
Returns the DB connection link. Connects to the DB if not connected. Does not
check if the connection has since been broken.
Returns
resource|mysqli Connection resource or object
|
public
|
#
forceReconnect( )
Reconnects to the database with the last saved connection details.
Reconnects to the database with the last saved connection details.
|
public
AMysql_Statement
|
#
setAutoPingSeconds( integer|false $autoPingSeconds )
Sets the amount of seconds needed to pass - since last communicating with the
database - before communicating again, to ping the database server beforehand
and also reconnect if the connection has been since lost.
Sets the amount of seconds needed to pass - since last communicating with the
database - before communicating again, to ping the database server beforehand
and also reconnect if the connection has been since lost.
Parameters
- $autoPingSeconds
integer|false $autoPingSeconds The amount of seconds, or FALSE if it should be disabled
Returns
|
public
AMysql_Abstract
|
#
setAutoReconnect( boolean $autoReconnect )
Parameters
Returns
See
$this->autoReconnect
|
public
boolean
|
|
public
AMysql_Abstract
|
#
setDefaultAutoCommit( boolean $defaultAutoCommit )
Parameters
- $defaultAutoCommit
boolean
Returns
See
$this->defaultAutoCommit
|
public
string
|
#
getFetchMode( )
Returns the default fetch mode. See the FETCH_ class constants.
Returns the default fetch mode. See the FETCH_ class constants.
Returns
string
|
public
AMysql_Abstract
|
#
selectDb( string $db )
Selects the given database.
Selects the given database.
Parameters
Returns
|
public
AMysql_Abstract
|
#
setUtf8( )
Takes care of setting everything to utf-8
Takes care of setting everything to utf-8
Returns
|
public
AMysql_Abstract
|
#
setCharset( string $charset )
Changes the character set of the connection.
Changes the character set of the connection.
Parameters
- $charset
string $charset Example: utf8
Returns
|
public
AMysql_Abstract
|
#
setNames( string $names )
Performs SET NAMES <charset> to change the character set. It may be
enough to use $this->setCharset().
Performs SET NAMES <charset> to change the character set. It may be
enough to use $this->setCharset().
Parameters
- $names
string $names Example: utf8
Returns
|
public
AMysql_Abstract
|
#
setAnsi( boolean $isAnsi )
Sets whether ANSI mode is on. Does not change the database's mode, you just
have to call this method when you know ANSI mode is being used in the mysql
connection, so that escaping is done according to the correct mode. ANSI mode is
off my default on most mysql installations.
Sets whether ANSI mode is on. Does not change the database's mode, you just
have to call this method when you know ANSI mode is being used in the mysql
connection, so that escaping is done according to the correct mode. ANSI mode is
off my default on most mysql installations.
Parameters
Returns
|
public
boolean
|
#
isAnsi( )
Returns whether ANSI mode is being used by this instance (not the
database)
Returns whether ANSI mode is being used by this instance (not the
database)
Returns
boolean
|
public static
string
|
#
escapeIdentifierSimple( string $identifier, string $qc = '`' )
Does a simple identifier escape. It should be fail proof for that literal
identifier.
Does a simple identifier escape. It should be fail proof for that literal
identifier.
Parameters
- $identifier
string $identifier The identifier
- $qc
string $qc The quote character. Default: `
Returns
string The escaped identifier.
|
protected static
string
|
#
_escapeIdentifier( string $identifier, string $qc )
Escapes an identifier. If there's a dot in it, it is split into two
identifiers, each escaped, and joined with a dot.
Escapes an identifier. If there's a dot in it, it is split into two
identifiers, each escaped, and joined with a dot.
Parameters
- $identifier
string $identifier The identifier
- $qc
string $qc The quote character. Default: `
Returns
string The escaped identifier.
|
public
string
|
#
escapeTable( string $tableName, string $as = null )
Escapes a table name.
Parameters
- $tableName
string $identifierName The identifier name. If it has a dot in it, it'll automatically
split the identifier name into the
tableName .columnName syntax.
- $as
string $as (Optional) adds an AS syntax, but only if it's a string. The value is the
alias the identifier should have for the query.
Returns
string
|
public
string
|
#
escapeColumn( string $columnName, string $as = null )
Escapes a column name.
Parameters
- $columnName
string $identifierName The identifier name. If it has a dot in it, it'll automatically
split the identifier name into the
tableName .columnName syntax.
- $as
string $as (Optional) adds an AS syntax, but only if it's a string. The value is the
alias the identifier should have for the query.
Returns
string
|
public
boolean
|
#
inTransaction( )
Returns whether a MySQL TRANSACTION is in progress, based off method
calls.
Returns whether a MySQL TRANSACTION is in progress, based off method
calls.
Returns
boolean
|
public
mixed
|
#
startTransaction( )
Performs a mysql ROLLBACK.
Performs a mysql ROLLBACK.
Returns
mixed Do not rely on this return value; it may change in the future.
|
public
mixed
|
#
commit( )
Performs a mysql COMMIT.
Returns
mixed Do not rely on this return value; it may change in the future.
|
public
mixed
|
#
rollback( )
Performs a mysql ROLLBACK.
Performs a mysql ROLLBACK.
Returns
mixed Do not rely on this return value; it may change in the future.
|
public
AMysql_Statement
|
#
query( string $sql, mixed $binds = array () )
Executes a query by an sql string and binds.
Executes a query by an sql string and binds.
Parameters
- $sql
string $sql The SQL string.
- $binds
mixed $binds The binds or a single bind.
Returns
|
public
string|integer
|
#
getOne( string $sql, mixed $binds = array () )
Executes a query, and returns the first found row's first column's value.
Throws a warning if no rows were found.
Executes a query, and returns the first found row's first column's value.
Throws a warning if no rows were found.
Parameters
- $sql
string $sql The SQL string.
- $binds
mixed $binds The binds or a single bind.
Returns
string|integer
|
public
string|integer|null
|
#
getOneNull( string $sql, mixed $binds = array () )
Like $this->getOne(), except returns a null when no result is found,
without throwing an error.
Like $this->getOne(), except returns a null when no result is found,
without throwing an error.
Parameters
- $sql
string $sql The SQL string.
- $binds
mixed $binds The binds or a single bind.
Returns
string|integer|null
See
|
public
integer
|
#
getOneInt( string $sql, mixed $binds = array () )
Like $this->getOne(), but casts the result to an int. No exception is
thrown when there is no result.
Like $this->getOne(), but casts the result to an int. No exception is
thrown when there is no result.
Parameters
- $sql
string $sql The SQL string.
- $binds
mixed $binds The binds or a single bind.
Returns
integer
See
|
public
AMysql_Statement
|
#
prepare( string $sql )
Prepares a mysql statement. It is to be executed.
Prepares a mysql statement. It is to be executed.
Parameters
- $sql
string $sql The unbound SQL string.
Returns
|
public
AMysql_Select
|
#
select( mixed $columns = null )
Returns a new instance of AMysql_Select. By passing parameters, you can now
have $select->column() invoked straight away (e.g.
$amysql->select('*')->...)
Returns a new instance of AMysql_Select. By passing parameters, you can now
have $select->column() invoked straight away (e.g.
$amysql->select('*')->...)
Parameters
- $columns
mixed $columns (Optional)
Returns
See
|
public
AMysql_Statement
|
#
newStatement( )
Creates a new AMysql_Statement instance and returns it.
Creates a new AMysql_Statement instance and returns it.
Returns
|
public
AMysql_Statement
|
#
upd( string $tableName, array $data, string $where, mixed $binds = array() )
Performs an instant UPDATE returning the statement.
Performs an instant UPDATE returning the statement.
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.
- $binds
mixed $binds (Optional) The binds or a single bind for the WHERE clause.
Returns
|
public
boolean
|
#
update( string $tableName, array $data, string $where, mixed $binds = array() )
Performs an instant UPDATE returning its success.
Performs an instant UPDATE returning its success.
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.
- $binds
mixed $binds (Optional) The binds or a single bind for the WHERE clause.
Returns
boolean Whether the update was successful.
|
public
boolean
|
#
updateMultipleByData( string $tableName, array $data, string $column = 'id' )
Updates multiple rows. The number of total affected rows can be found in
$this->multipleAffectedRows.
Updates multiple rows. The number of total affected rows can be found in
$this->multipleAffectedRows.
Parameters
- $tableName
string $tableName The table name.
- $data
array[] $data The array of data changes. An array of an array of column-value pairs to
update for that row. One of the column keys should be the one with the value to
search for for updating (typically the primary key) e.g. [ [ 'id' => 1,
'col1' => 'newStringValue', 'col2' => 'newStringValue2' ], [ 'id' => 2,
'col1' => 'anotherNewStringValue', 'col2' => 'anotherNewStringValue2' ],
... ]
- $column
string $column (Options) the name of the column to search for, and key to use among the
data. The default is 'id'.
Returns
boolean Whether there was an equal amount of successful updates (whether a row was
affected or not) as the size of the inputted data array.
|
public
boolean
|
#
updateMultipleByKey( string $tableName, array $data, string $column = 'id', boolean $updateSameColumn = false )
Updates multiple rows. The values for the column to search for is the key of
each row. The number of total affected rows can be found in
$this->multipleAffectedRows.
Updates multiple rows. The values for the column to search for is the key of
each row. The number of total affected rows can be found in
$this->multipleAffectedRows.
Parameters
- $tableName
string $tableName The table name.
- $data
array[] $data The array of data changes. An array indexed by the value of the column to
apply the update to (typically the primary) key containing an array of
column-value pairs to update for that row. e.g. [ 1 => [ 'col1' =>
'newStringValue', 'col2' => 'newStringValue2' ], 2 => [ 'id' => 2,
'col1' => 'anotherNewStringValue', 'col2' => 'anotherNewStringValue2' ],
... ]
- $column
string $column (Options) the name of the column and key to search for. The default is
'id'.
- $updateSameColumn
boolean $updateSameColumn (Optional) If the column being searched for is within the a
data row, if this is false, that key should be removed before updating the data.
This is the default.
Returns
boolean
|
public
AMysql_Statement
|
#
ins( string $tableName, array $data )
Performs an instant INSERT, returning the statement.
Performs an instant INSERT, returning the statement.
Parameters
- $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
|
public
integer|boolean
|
#
insert( string $tableName, array $data )
Performs an instant INSERT, but tries to return the last insert id straight
away.
Performs an instant INSERT, but tries to return the last insert id straight
away.
Parameters
- $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
integer|boolean The mysql_insert_id(), if the query succeeded and there exists a primary key.
Otherwise the boolean of whether the insert was successful.
|
public
AMysql_Statement
|
#
rep( string $tableName, array $data )
Performs an instant REPLACE, returning the statement.
Performs an instant REPLACE, returning the statement.
Parameters
- $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
|
public
boolean
|
#
replace( string $tableName, array $data )
Performs an instant REPLACE, returning its success.
Performs an instant REPLACE, returning its success.
Parameters
- $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
boolean Success.
|
public
integer
|
#
save( mixed $tableName, mixed $data, mixed $columnName, mixed $value = null )
Performs an INSERT or an UPDATE; if the $value parameter is not falsy, an
UPDATE is performed with the given column name and value, otherwise an insert.
It is recommended that this is used for tables with a primary key, and use the
primary key as the column to look at. Also, this would keep the return value
consistent.
Performs an INSERT or an UPDATE; if the $value parameter is not falsy, an
UPDATE is performed with the given column name and value, otherwise an insert.
It is recommended that this is used for tables with a primary key, and use the
primary key as the column to look at. Also, this would keep the return value
consistent.
Parameters
- $tableName
mixed $tableName The table name to INSERT or UPDATE to
- $data
mixed $data The data to change
- $columnName
mixed $columnName The column to search by. It should be a primary key.
- $value
mixed $value (Optional) The value to look for in case you want to UPDATE. Keep this at
null, 0, or anything else falsy for INSERT.
Returns
integer If the $value is not falsy, it returns $value after UPDATING. Otherwise the
mysql_insert_id() of the newly INSERTED row.
|
public
AMysql_Statement
|
#
del( string $tableName, string $where, mixed $binds = array () )
Performs an instant DELETE, returning the statement.
Performs an instant DELETE, returning the statement.
Parameters
- $tableName
string $tableName The table name.
- $where
string $where An SQL substring of the WHERE clause.
- $binds
mixed $binds (Optional) The binds or a single bind for the WHERE clause.
Returns
|
public
resource|false
|
#
delete( string $tableName, string $where, mixed $binds = array () )
Performs an instant DELETE, returning whether it succeeded.
Performs an instant DELETE, returning whether it succeeded.
Parameters
- $tableName
string $tableName The table name.
- $where
string $where An SQL substring of the WHERE clause.
- $binds
mixed $binds (Optional) The binds or a single bind for the WHERE clause.
Returns
resource|false The mysql resource if the delete was successful, otherwise false.
|
public
integer
|
#
foundRows( )
If the last mysql query was a SELECT with the SQL_CALC_FOUND_ROWS options,
this returns the number of found rows from that last query with LIMIT and OFFSET
ignored.
If the last mysql query was a SELECT with the SQL_CALC_FOUND_ROWS options,
this returns the number of found rows from that last query with LIMIT and OFFSET
ignored.
Returns
integer Number of found rows.
|
public
AMysql_Expr
|
#
expr( )
Returns an AMysql_Expr for using in prepared statements as values. See the
AMysql_Expr class for details. To bind a literal value without apostrophes, here
is an example of how you can execute a prepared statement with the help of
placeholders: $amysql->prepare('SELECT ? AS time')->execute(array (
$amysql->expr('CURRENT_TIMESTAMP') ))
Returns an AMysql_Expr for using in prepared statements as values. See the
AMysql_Expr class for details. To bind a literal value without apostrophes, here
is an example of how you can execute a prepared statement with the help of
placeholders: $amysql->prepare('SELECT ? AS time')->execute(array (
$amysql->expr('CURRENT_TIMESTAMP') ))
Returns
See
|
public static
|
#
escapeLike( string $s, string $escapeStr = '=' )
Escapes LIKE. The after the LIKE <string> syntax, you must place an
ESCAPE statement with '=' or whatever you pass here as $escapeStr It is not
recommended to call this static method externally; please use AMysql_Expr instead.
Escapes LIKE. The after the LIKE <string> syntax, you must place an
ESCAPE statement with '=' or whatever you pass here as $escapeStr It is not
recommended to call this static method externally; please use AMysql_Expr instead.
Parameters
- $s
string $s The string to LIKE escape
- $escapeStr
string $escapeChar (Optional) The escape character
|
public
string|integer
|
#
escape( mixed $value )
Escapes a value. The method depends on the passed value's type, but unless
the passed type is an AMysql_Expr, the safety is almost guaranteed. Do not put
apostrophes around bind marks! Those are handled by this escaping method.
Escapes a value. The method depends on the passed value's type, but unless
the passed type is an AMysql_Expr, the safety is almost guaranteed. Do not put
apostrophes around bind marks! Those are handled by this escaping method.
Parameters
- $value
mixed The value to escape
Returns
string|integer The value safe to put into a query, including surrounding apostrophes if the
value is string-like, not including apostrophes if it's an int or an AMysql_Expr or AMysql_Select (for selectception)
|
public static
array
|
#
transpose( array $array )
Transposes a 2 dimensional array. Every inner array must contain the same
keys as the other inner arrays, otherwise unexpected results may occur.
Transposes a 2 dimensional array. Every inner array must contain the same
keys as the other inner arrays, otherwise unexpected results may occur.
Example: $input = array ( 3 => array ( 'col1' => 'bla', 'col2' =>
'yo' ), 9 => array ( 'col1' => 'ney', 'col2' => 'lol' ) ); $output =
$amysql->transpose($input);
$output: array ( 'col1' => array ( 3 => 'bla', 9 => 'ney' ), 'col2'
=> array ( 3 => 'yo', 9 => 'lol' ) );
Parameters
- $array
array $array The 2 dimensional array to transpose
Returns
array
|
public
AMysql_Abstract
|
#
addQuery( string $query, float $queryTime )
Adds a query and a profile for it to the list of queries. Used by
AMysql_Statement. Do not call externally!
Adds a query and a profile for it to the list of queries. Used by
AMysql_Statement. Do not call externally!
Parameters
- $query
string $query The SQL query.
- $queryTime
float $queryTime The time the query took.
Returns
|
public
boolean
|
#
pingReconnect( )
Pings the mysql server to see if it's still alive; attempts to reconnect
otherwise.
Pings the mysql server to see if it's still alive; attempts to reconnect
otherwise.
Returns
boolean TRUE if the connection is still there or reconnection was successful. FALSE if
reconnection wasn't successful.
|
public
AMysql_Abstract
|
#
autoPing( )
Pings if the set amount of auto ping time has passed.
Pings if the set amount of auto ping time has passed.
Returns
|
public
Mysqli|resource
|
#
autoPingConnect( )
Auto connects and auto pings (if on), returning the mysql link.
Auto connects and auto pings (if on), returning the mysql link.
Returns
Mysqli|resource
|
public
array
|
#
getQueries( )
Gets the list of SQL queries performed so far by AMysql_Statement objects
connected by this object.
Gets the list of SQL queries performed so far by AMysql_Statement objects
connected by this object.
Returns
array
|
public
array[]
|
#
getQueriesData( )
Returns an arrays of profiled query data. Each value is an array that
consists of:
- query - The SQL query performed
- time - The amount of seconds the query took (float)
Returns an arrays of profiled query data. Each value is an array that
consists of: - query - The SQL query performed - time - The amount of seconds
the query took (float)
If profileQueries wss off at any query, its time value will be null.
Returns
array[]
|
public
AMysql_Profiler
|
#
getProfiler( )
Returns the profiler object. Pass it to your templates to retrieve the
profiler results.
Returns the profiler object. Pass it to your templates to retrieve the
profiler results.
Returns
|
public
AMysql_Abstract
|
#
useNewProfiler( )
Force using a new profiler.
Force using a new profiler.
Returns
|
public
|
#
handleError( mixed $msg, mixed $code, mixed $query )
For internal use.
Parameters
- $msg
mixed $msg
- $code
mixed $code
- $query
mixed $query
Throws
|
public
|
|
public
|
|