Since: 8.4

class Mysql extends PDO (View source)

Constants

PARAM_NULL

Represents the SQL NULL data type.

PARAM_INT

Represents the SQL INTEGER data type.

PARAM_STR

Represents the SQL CHAR, VARCHAR, or other string data type.

PARAM_LOB

Represents the SQL large object data type.

PARAM_STMT

Represents a recordset type. Not currently supported by any drivers.

PARAM_BOOL

Represents a boolean data type.

PARAM_STR_NATL Since: 7.2

Flag to denote a string uses the national character set.

PARAM_STR_CHAR Since: 7.2

Flag to denote a string uses the regular character set.

ATTR_DEFAULT_STR_PARAM Since: 7.2

Sets the default string parameter type, this can be one of PDO::PARAM_STR_NATL and PDO::PARAM_STR_CHAR.

SQLITE_DETERMINISTIC Since: 7.1.4

Specifies that a function created with PDO::sqliteCreateFunction() is deterministic, i.e. it always returns the same result given the same inputs within a single SQL statement.

SQLITE_OPEN_READONLY Since: 7.3

SQLITE_OPEN_READWRITE Since: 7.3

SQLITE_OPEN_CREATE Since: 7.3

SQLITE_ATTR_OPEN_FLAGS Since: 7.3

PARAM_INPUT_OUTPUT

Specifies that the parameter is an INOUT parameter for a stored procedure. You must bitwise-OR this value with an explicit PDO::PARAM_* data type.

PARAM_EVT_ALLOC

Allocation event

PARAM_EVT_FREE

Deallocation event

PARAM_EVT_EXEC_PRE

Event triggered prior to execution of a prepared statement.

PARAM_EVT_EXEC_POST

Event triggered subsequent to execution of a prepared statement.

PARAM_EVT_FETCH_PRE

Event triggered prior to fetching a result from a resultset.

PARAM_EVT_FETCH_POST

Event triggered subsequent to fetching a result from a resultset.

PARAM_EVT_NORMALIZE

Event triggered during bound parameter registration allowing the driver to normalize the parameter name.

FETCH_LAZY

Specifies that the fetch method shall return each row as an object with variable names that correspond to the column names returned in the result set. PDO::FETCH_LAZY creates the object variable names as they are accessed.

Not valid inside PDOStatement::fetchAll.

FETCH_ASSOC

Specifies that the fetch method shall return each row as an array indexed by column name as returned in the corresponding result set. If the result set contains multiple columns with the same name, PDO::FETCH_ASSOC returns only a single value per column name.

FETCH_NUM

Specifies that the fetch method shall return each row as an array indexed by column number as returned in the corresponding result set, starting at column 0.

FETCH_BOTH

Specifies that the fetch method shall return each row as an array indexed by both column name and number as returned in the corresponding result set, starting at column 0.

FETCH_OBJ

Specifies that the fetch method shall return each row as an object with property names that correspond to the column names returned in the result set.

FETCH_BOUND

Specifies that the fetch method shall return TRUE and assign the values of the columns in the result set to the PHP variables to which they were bound with the PDOStatement::bindParam or PDOStatement::bindColumn methods.

FETCH_COLUMN

Specifies that the fetch method shall return only a single requested column from the next row in the result set.

FETCH_CLASS

Specifies that the fetch method shall return a new instance of the requested class, mapping the columns to named properties in the class.

The magic __set method is called if the property doesn't exist in the requested class

FETCH_INTO

Specifies that the fetch method shall update an existing instance of the requested class, mapping the columns to named properties in the class.

FETCH_FUNC

Allows completely customize the way data is treated on the fly (only valid inside PDOStatement::fetchAll).

FETCH_GROUP

Group return by values. Usually combined with PDO::FETCH_COLUMN or PDO::FETCH_KEY_PAIR.

FETCH_UNIQUE

Fetch only the unique values.

FETCH_KEY_PAIR Since: 5.2.3

Fetch a two-column result into an array where the first column is a key and the second column is the value.

FETCH_CLASSTYPE

Determine the class name from the value of first column.

FETCH_SERIALIZE

As PDO::FETCH_INTO but object is provided as a serialized string.

Available since PHP 5.1.0. Since PHP 5.3.0 the class constructor is never called if this flag is set.

FETCH_PROPS_LATE Since: 5.2

Call the constructor before setting properties.

FETCH_NAMED

Specifies that the fetch method shall return each row as an array indexed by column name as returned in the corresponding result set. If the result set contains multiple columns with the same name, PDO::FETCH_NAMED returns an array of values per column name.

ATTR_AUTOCOMMIT

If this value is FALSE, PDO attempts to disable autocommit so that the connection begins a transaction.

ATTR_PREFETCH

Setting the prefetch size allows you to balance speed against memory usage for your application. Not all database/driver combinations support setting of the prefetch size. A larger prefetch size results in increased performance at the cost of higher memory usage.

ATTR_TIMEOUT

Sets the timeout value in seconds for communications with the database.

ATTR_ERRMODE

ATTR_SERVER_VERSION

This is a read only attribute; it will return information about the version of the database server to which PDO is connected.

ATTR_CLIENT_VERSION

This is a read only attribute; it will return information about the version of the client libraries that the PDO driver is using.

ATTR_SERVER_INFO

This is a read only attribute; it will return some meta information about the database server to which PDO is connected.

ATTR_CONNECTION_STATUS

ATTR_CASE

Force column names to a specific case specified by the PDO::CASE_* constants.

ATTR_CURSOR_NAME

Get or set the name to use for a cursor. Most useful when using scrollable cursors and positioned updates.

ATTR_CURSOR

Selects the cursor type. PDO currently supports either PDO::CURSOR_FWDONLY and PDO::CURSOR_SCROLL. Stick with PDO::CURSOR_FWDONLY unless you know that you need a scrollable cursor.

ATTR_ORACLE_NULLS

Convert empty strings to SQL NULL values on data fetches.

ATTR_PERSISTENT

Request a persistent connection, rather than creating a new connection.

ATTR_STATEMENT_CLASS

Sets the class name of which statements are returned as.

ATTR_FETCH_TABLE_NAMES

Prepend the containing table name to each column name returned in the result set. The table name and column name are separated by a decimal (.) character. Support of this attribute is at the driver level; it may not be supported by your driver.

ATTR_FETCH_CATALOG_NAMES

Prepend the containing catalog name to each column name returned in the result set. The catalog name and column name are separated by a decimal (.) character. Support of this attribute is at the driver level; it may not be supported by your driver.

ATTR_DRIVER_NAME

Returns the name of the driver.

using PDO::ATTR_DRIVER_NAME ``` if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') { echo "Running on mysql; doing something mysql specific here\n"; } ```

ATTR_STRINGIFY_FETCHES

Forces all values fetched to be treated as strings.

ATTR_MAX_COLUMN_LEN

Sets the maximum column name length.

ATTR_EMULATE_PREPARES Since: 5.1.3

ATTR_DEFAULT_FETCH_MODE Since: 5.2

ERRMODE_SILENT

Do not raise an error or exception if an error occurs. The developer is expected to explicitly check for errors. This is the default mode.

ERRMODE_WARNING

Issue a PHP E_WARNING message if an error occurs.

ERRMODE_EXCEPTION

Throw a PDOException if an error occurs.

CASE_NATURAL

Leave column names as returned by the database driver.

CASE_LOWER

Force column names to lower case.

CASE_UPPER

Force column names to upper case.

NULL_NATURAL

NULL_EMPTY_STRING

NULL_TO_STRING

ERR_NONE

Corresponds to SQLSTATE '00000', meaning that the SQL statement was successfully issued with no errors or warnings. This constant is for your convenience when checking PDO::errorCode or PDOStatement::errorCode to determine if an error occurred. You will usually know if this is the case by examining the return code from the method that raised the error condition anyway.

FETCH_ORI_NEXT

Fetch the next row in the result set. Valid only for scrollable cursors.

FETCH_ORI_PRIOR

Fetch the previous row in the result set. Valid only for scrollable cursors.

FETCH_ORI_FIRST

Fetch the first row in the result set. Valid only for scrollable cursors.

FETCH_ORI_LAST

Fetch the last row in the result set. Valid only for scrollable cursors.

FETCH_ORI_ABS

Fetch the requested row by row number from the result set. Valid only for scrollable cursors.

FETCH_ORI_REL

Fetch the requested row by relative position from the current position of the cursor in the result set. Valid only for scrollable cursors.

FETCH_DEFAULT Since: 8.0.7

Specifies that the default fetch mode shall be used.

CURSOR_FWDONLY

Create a PDOStatement object with a forward-only cursor. This is the default cursor choice, as it is the fastest and most common data access pattern in PHP.

CURSOR_SCROLL

Create a PDOStatement object with a scrollable cursor. Pass the PDO::FETCHORI* constants to control the rows fetched from the result set.

MYSQL_ATTR_USE_BUFFERED_QUERY

If this attribute is set to TRUE on a PDOStatement, the MySQL driver will use the buffered versions of the MySQL API. If you're writing portable code, you should use PDOStatement::fetchAll instead.

Forcing queries to be buffered in mysql ``` if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') { $stmt = $db->prepare('select * from foo', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); } else { die("my application only works with mysql; I should use \$stmt->fetchAll() instead"); } ```

MYSQL_ATTR_LOCAL_INFILE

Enable LOAD LOCAL INFILE.

Note, this constant can only be used in the driver_options array when constructing a new database handle.

MYSQL_ATTR_INIT_COMMAND

Command to execute when connecting to the MySQL server. Will automatically be re-executed when reconnecting.

Note, this constant can only be used in the driver_options array when constructing a new database handle.

MYSQL_ATTR_MAX_BUFFER_SIZE

Maximum buffer size. Defaults to 1 MiB. This constant is not supported when compiled against mysqlnd.

MYSQL_ATTR_READ_DEFAULT_FILE

Read options from the named option file instead of from my.cnf. This option is not available if mysqlnd is used, because mysqlnd does not read the mysql configuration files.

MYSQL_ATTR_READ_DEFAULT_GROUP

Read options from the named group from my.cnf or the file specified with MYSQL_READ_DEFAULT_FILE. This option is not available if mysqlnd is used, because mysqlnd does not read the mysql configuration files.

MYSQL_ATTR_COMPRESS

Enable network communication compression. This is not supported when compiled against mysqlnd.

MYSQL_ATTR_DIRECT_QUERY

Perform direct queries, don't use prepared statements.

MYSQL_ATTR_FOUND_ROWS

Return the number of found (matched) rows, not the number of changed rows.

MYSQL_ATTR_IGNORE_SPACE

Permit spaces after function names. Makes all functions names reserved words.

MYSQL_ATTR_SERVER_PUBLIC_KEY

MYSQL_ATTR_SSL_KEY Since: 5.3.7

The file path to the SSL key.

MYSQL_ATTR_SSL_CERT Since: 5.3.7

The file path to the SSL certificate.

MYSQL_ATTR_SSL_CA Since: 5.3.7

The file path to the SSL certificate authority.

MYSQL_ATTR_SSL_CAPATH Since: 5.3.7

The file path to the directory that contains the trusted SSL CA certificates, which are stored in PEM format.

MYSQL_ATTR_SSL_CIPHER Since: 5.3.7

A list of one or more permissible ciphers to use for SSL encryption, in a format understood by OpenSSL.

For example: DHE-RSA-AES256-SHA:AES128-SHA

MYSQL_ATTR_MULTI_STATEMENTS Since: 5.5.21

Disables multi query execution in both {[PDO](https://www.php.net/PDO) and {[PDO](https://www.php.net/PDO) when set to FALSE.

Note, this constant can only be used in the driver_options array when constructing a new database handle.

MYSQL_ATTR_SSL_VERIFY_SERVER_CERT Since: 7.0.18

Disables SSL peer verification when set to FALSE.

MYSQL_ATTR_LOCAL_INFILE_DIRECTORY Since: 8.1

PGSQL_ASSOC

PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT

PGSQL_ATTR_DISABLE_PREPARES Since: 5.6

PGSQL_BAD_RESPONSE

PGSQL_BOTH

PGSQL_TRANSACTION_IDLE

PGSQL_TRANSACTION_ACTIVE

PGSQL_TRANSACTION_INTRANS

PGSQL_TRANSACTION_INERROR

PGSQL_TRANSACTION_UNKNOWN

PGSQL_CONNECT_ASYNC

PGSQL_CONNECT_FORCE_NEW

PGSQL_CONNECTION_AUTH_OK

PGSQL_CONNECTION_AWAITING_RESPONSE

PGSQL_CONNECTION_BAD

PGSQL_CONNECTION_OK

PGSQL_CONNECTION_MADE

PGSQL_CONNECTION_SETENV

PGSQL_CONNECTION_SSL_STARTUP

PGSQL_CONNECTION_STARTED

PGSQL_COMMAND_OK

PGSQL_CONV_FORCE_NULL

PGSQL_CONV_IGNORE_DEFAULT

PGSQL_CONV_IGNORE_NOT_NULL

PGSQL_COPY_IN

PGSQL_COPY_OUT

PGSQL_DIAG_CONTEXT

PGSQL_DIAG_INTERNAL_POSITION

PGSQL_DIAG_INTERNAL_QUERY

PGSQL_DIAG_MESSAGE_DETAIL

PGSQL_DIAG_MESSAGE_HINT

PGSQL_DIAG_MESSAGE_PRIMARY

PGSQL_DIAG_SEVERITY

PGSQL_DIAG_SOURCE_FILE

PGSQL_DIAG_SOURCE_FUNCTION

PGSQL_DIAG_SOURCE_LINE

PGSQL_DIAG_SQLSTATE

PGSQL_DIAG_STATEMENT_POSITION

PGSQL_DML_ASYNC

PGSQL_DML_EXEC

PGSQL_DML_NO_CONV

PGSQL_DML_STRING

PGSQL_DML_ESCAPE

PGSQL_EMPTY_QUERY

PGSQL_ERRORS_DEFAULT

PGSQL_ERRORS_TERSE

PGSQL_ERRORS_VERBOSE

PGSQL_FATAL_ERROR

PGSQL_NONFATAL_ERROR

PGSQL_NOTICE_ALL

PGSQL_NOTICE_CLEAR

PGSQL_NOTICE_LAST

PGSQL_NUM

PGSQL_POLLING_ACTIVE

PGSQL_POLLING_FAILED

PGSQL_POLLING_OK

PGSQL_POLLING_READING

PGSQL_POLLING_WRITING

PGSQL_SEEK_CUR

PGSQL_SEEK_END

PGSQL_SEEK_SET

PGSQL_STATUS_LONG

PGSQL_STATUS_STRING

PGSQL_TUPLES_OK

SQLSRV_TXN_READ_UNCOMMITTED

SQLSRV_TXN_READ_COMMITTED

SQLSRV_TXN_REPEATABLE_READ

SQLSRV_TXN_SNAPSHOT

SQLSRV_TXN_SERIALIZABLE

SQLSRV_ENCODING_BINARY

SQLSRV_ENCODING_SYSTEM

SQLSRV_ENCODING_UTF8

SQLSRV_ENCODING_DEFAULT

SQLSRV_ATTR_ENCODING

SQLSRV_ATTR_QUERY_TIMEOUT

SQLSRV_ATTR_DIRECT_QUERY

SQLSRV_ATTR_CURSOR_SCROLL_TYPE

SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE

SQLSRV_ATTR_FETCHES_NUMERIC_TYPE

SQLSRV_ATTR_FETCHES_DATETIME_TYPE

SQLSRV_ATTR_FORMAT_DECIMALS

SQLSRV_ATTR_DECIMAL_PLACES

SQLSRV_ATTR_DATA_CLASSIFICATION

SQLSRV_PARAM_OUT_DEFAULT_SIZE

SQLSRV_CURSOR_KEYSET

SQLSRV_CURSOR_DYNAMIC

SQLSRV_CURSOR_STATIC

SQLSRV_CURSOR_BUFFERED

SQLITE_ATTR_READONLY_STATEMENT Since: 7.4

SQLITE_ATTR_EXTENDED_RESULT_CODES Since: 7.4

OCI_ATTR_ACTION Since: 7.2.16

Provides a way to specify the action on the database session.

OCI_ATTR_CLIENT_INFO Since: 7.2.16

Provides a way to specify the client info on the database session.

OCI_ATTR_CLIENT_IDENTIFIER Since: 7.2.16

Provides a way to specify the client identifier on the database session.

OCI_ATTR_MODULE Since: 7.2.16

Provides a way to specify the module on the database session.

OCI_ATTR_CALL_TIMEOUT Since: 8.0

The number of milliseconds to wait for individual round trips to the database to complete before timing out.

FB_ATTR_DATE_FORMAT

Sets the date format.

FB_ATTR_TIME_FORMAT

Sets the time format.

FB_ATTR_TIMESTAMP_FORMAT

Sets the timestamp format.

ATTR_USE_BUFFERED_QUERY

ATTR_LOCAL_INFILE

ATTR_INIT_COMMAND

ATTR_MAX_BUFFER_SIZE

ATTR_READ_DEFAULT_FILE

ATTR_READ_DEFAULT_GROUP

ATTR_COMPRESS

ATTR_DIRECT_QUERY

ATTR_FOUND_ROWS

ATTR_IGNORE_SPACE

ATTR_SSL_KEY

ATTR_SSL_CERT

ATTR_SSL_CA

ATTR_SSL_CAPATH

ATTR_SSL_CIPHER

ATTR_SERVER_PUBLIC_KEY

ATTR_MULTI_STATEMENTS

ATTR_SSL_VERIFY_SERVER_CERT

ATTR_LOCAL_INFILE_DIRECTORY

Methods

__construct(string $dsn, string $username = null, string $password = null, array $options = null)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Creates a PDO instance representing a connection to a database

from  PDO
PDOStatement|false
prepare(string $query, array $options = [])

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Prepares a statement for execution and returns a statement object

from  PDO
bool
beginTransaction()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Initiates a transaction

Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO object instance are not committed until you end the transaction by calling [PDO](https://www.php.net/PDO).

from  PDO
bool
commit()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Commits a transaction

from  PDO
bool
rollBack()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Rolls back a transaction

from  PDO
bool
inTransaction()

(PHP 5 >= 5.3.3, Bundled pdo_pgsql, PHP 7)
Checks if inside a transaction

from  PDO
bool
setAttribute(int $attribute, mixed $value)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Set an attribute

from  PDO
int|false
exec(string $statement)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Execute an SQL statement and return the number of affected rows

from  PDO
PDOStatement|false
query(string $query, int|null $fetchMode = null, ...$fetchModeArgs)

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.2.0)
Executes an SQL statement, returning a result set as a PDOStatement object

from  PDO
string|false
lastInsertId(string $name = null)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Returns the ID of the last inserted row or sequence value

from  PDO
string|null
errorCode()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Fetch the SQLSTATE associated with the last operation on the database handle

from  PDO
array
errorInfo()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Fetch extended error information associated with the last operation on the database handle

from  PDO
mixed
getAttribute(int $attribute)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.2.0)
Retrieve a database connection attribute

from  PDO
string|false
quote(string $string, int $type = PDO::PARAM_STR)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.2.1)
Quotes a string for use in a query.

from  PDO
__wakeup()

No description

from  PDO
__sleep()

No description

from  PDO
static array
getAvailableDrivers()

(PHP 5 >= 5.1.3, PHP 7, PECL pdo >= 1.0.3)
Return an array of available PDO drivers

from  PDO
bool
sqliteCreateAggregate(string $function_name, callable $step_func, callable $finalize_func, int $num_args = -1)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo_sqlite >= 1.0.0)
Registers an aggregating User Defined Function for use in SQL statements

from  PDO
bool
sqliteCreateCollation(string $name, callable $callback)

(PHP 5 >= 5.3.11, PHP 7)
Registers a User Defined Function for use as a collating function in SQL statements

from  PDO
bool
sqliteCreateFunction(string $function_name, callable $callback, int $num_args = -1, int $flags = 0)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo_sqlite >= 1.0.0)
Registers a User Defined Function for use in SQL statements

from  PDO
bool
pgsqlCopyFromArray(string $tableName, array $rows, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from PHP array into table

from  PDO
bool
pgsqlCopyFromFile(string $tableName, string $filename, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from file into table

from  PDO
array|false
pgsqlCopyToArray(string $tableName, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from database table into PHP array

from  PDO
bool
pgsqlCopyToFile(string $tableName, string $filename, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from table into file

from  PDO
string|false
pgsqlLOBCreate()

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL pdo_pgsql >= 1.0.2)
Creates a new large object

from  PDO
resource|false
pgsqlLOBOpen(string $oid, string $mode = "rb")

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL pdo_pgsql >= 1.0.2)
Opens an existing large object stream

from  PDO
bool
pgsqlLOBUnlink(string $oid)

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL pdo_pgsql >= 1.0.2)
Deletes the large object

from  PDO
array|false
pgsqlGetNotify(int $fetchMode = PDO::FETCH_DEFAULT, int $timeoutMilliseconds = 0)

(PHP 5 >= 5.6.0, PHP 7, PHP 8)
Get asynchronous notification

from  PDO
int
pgsqlGetPid()

(PHP 5 >= 5.6.0, PHP 7, PHP 8)
Get the server PID

from  PDO
static PDO
connect(string $dsn, string|null $username = null, string|null $password = null, array|null $options = null)

No description

from  PDO
int
getWarningCount()

No description

Details

__construct(string $dsn, string $username = null, string $password = null, array $options = null)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Creates a PDO instance representing a connection to a database

Parameters

string $dsn
string $username [optional]
string $password [optional]
array $options [optional]

Exceptions

PDOException

PDOStatement|false prepare(string $query, array $options = [])

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Prepares a statement for execution and returns a statement object

Parameters

string $query

This must be a valid SQL statement for the target database server.

array $options

[optional]

This array holds one or more key=>value pairs to set attribute values for the PDOStatement object that this method returns. You would most commonly use this to set the PDO::ATTR_CURSOR value to PDO::CURSOR_SCROLL to request a scrollable cursor. Some drivers have driver specific options that may be set at prepare-time.

Return Value

PDOStatement|false

If the database server successfully prepares the statement, PDO::prepare returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare returns FALSE or emits PDOException (depending on error handling).

Emulated prepared statements does not communicate with the database server so PDO::prepare does not check the statement.

bool beginTransaction()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Initiates a transaction

Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO object instance are not committed until you end the transaction by calling [PDO](https://www.php.net/PDO).

Calling PDO will roll back all changes to the database and return the connection to autocommit mode.

Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit COMMIT will prevent you from rolling back any other changes within the transaction boundary.

Return Value

bool

TRUE on success or FALSE on failure.

Exceptions

PDOException

bool commit()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Commits a transaction

Return Value

bool

TRUE on success or FALSE on failure.

Exceptions

PDOException

bool rollBack()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Rolls back a transaction

Return Value

bool

TRUE on success or FALSE on failure.

Exceptions

PDOException

bool inTransaction()

(PHP 5 >= 5.3.3, Bundled pdo_pgsql, PHP 7)
Checks if inside a transaction

Return Value

bool

TRUE if a transaction is currently active, and FALSE if not.

bool setAttribute(int $attribute, mixed $value)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Set an attribute

Parameters

int $attribute
mixed $value

Return Value

bool

TRUE on success or FALSE on failure.

int|false exec(string $statement)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Execute an SQL statement and return the number of affected rows

Parameters

string $statement

The SQL statement to prepare and execute.

Data inside the query should be properly escaped.

Return Value

int|false

PDO::exec returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, PDO::exec returns 0.

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

The following example incorrectly relies on the return value of PDO::exec, wherein a statement that affected 0 rows results in a call to die: ``` $db->exec() or die(print_r($db->errorInfo(), true)); ```

PDOStatement|false query(string $query, int|null $fetchMode = null, ...$fetchModeArgs)

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.2.0)
Executes an SQL statement, returning a result set as a PDOStatement object

Parameters

string $query

The SQL statement to prepare and execute.

Data inside the query should be properly escaped.

int|null $fetchMode

The fetch mode must be one of the PDO::FETCH_* constants.

...$fetchModeArgs

Return Value

PDOStatement|false

PDO::query returns a PDOStatement object, or FALSE on failure.

See also

PDOStatement::setFetchMode For a full description of the second and following parameters.

string|false lastInsertId(string $name = null)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Returns the ID of the last inserted row or sequence value

Parameters

string $name

[optional]

Name of the sequence object from which the ID should be returned.

Return Value

string|false

If a sequence name was not specified for the name parameter, PDO::lastInsertId returns a string representing the row ID of the last row that was inserted into the database.

If a sequence name was specified for the name parameter, PDO::lastInsertId returns a string representing the last value retrieved from the specified sequence object.

If the PDO driver does not support this capability, PDO::lastInsertId triggers an IM001 SQLSTATE.

Exceptions

PDOException

string|null errorCode()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Fetch the SQLSTATE associated with the last operation on the database handle

Return Value

string|null

an SQLSTATE, a five characters alphanumeric identifier defined in the ANSI SQL-92 standard. Briefly, an SQLSTATE consists of a two characters class value followed by a three characters subclass value. A class value of 01 indicates a warning and is accompanied by a return code of SQL_SUCCESS_WITH_INFO. Class values other than '01', except for the class 'IM', indicate an error. The class 'IM' is specific to warnings and errors that derive from the implementation of PDO (or perhaps ODBC, if you're using the ODBC driver) itself. The subclass value '000' in any class indicates that there is no subclass for that SQLSTATE.

PDO::errorCode only retrieves error codes for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare or PDO::query and invoke an error on the statement handle, PDO::errorCode will not reflect that error. You must call PDOStatement::errorCode to return the error code for an operation performed on a particular statement handle.

Returns NULL if no operation has been run on the database handle.

array errorInfo()

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
Fetch extended error information associated with the last operation on the database handle

Return Value

If the SQLSTATE error code is not set or there is no driver-specific error, the elements following element 0 will be set to NULL.

PDO::errorInfo only retrieves error information for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare or PDO::query and invoke an error on the statement handle, PDO::errorInfo will not reflect the error from the statement handle. You must call PDOStatement::errorInfo to return the error information for an operation performed on a particular statement handle.

array

PDO::errorInfo returns an array of error information about the last operation performed by this database handle. The array consists of the following fields:

Element Information
0 SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard).
1 Driver-specific error code.
2 Driver-specific error message.

mixed getAttribute(int $attribute)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.2.0)
Retrieve a database connection attribute

Parameters

int $attribute

One of the PDO::ATTR_* constants. The constants that apply to database connections are as follows: PDO::ATTR_AUTOCOMMIT PDO::ATTR_CASE PDO::ATTR_CLIENT_VERSION PDO::ATTR_CONNECTION_STATUS PDO::ATTR_DRIVER_NAME PDO::ATTR_ERRMODE PDO::ATTR_ORACLE_NULLS PDO::ATTR_PERSISTENT PDO::ATTR_PREFETCH PDO::ATTR_SERVER_INFO PDO::ATTR_SERVER_VERSION PDO::ATTR_TIMEOUT

Return Value

mixed

A successful call returns the value of the requested PDO attribute. An unsuccessful call returns null.

string|false quote(string $string, int $type = PDO::PARAM_STR)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.2.1)
Quotes a string for use in a query.

Parameters

string $string

The string to be quoted.

int $type

[optional]

Provides a data type hint for drivers that have alternate quoting styles.

Return Value

string|false

a quoted string that is theoretically safe to pass into an SQL statement. Returns FALSE if the driver does not support quoting in this way.

final __wakeup()

No description

final __sleep()

No description

static array getAvailableDrivers()

(PHP 5 >= 5.1.3, PHP 7, PECL pdo >= 1.0.3)
Return an array of available PDO drivers

Return Value

array

PDO::getAvailableDrivers returns an array of PDO driver names. If no drivers are available, it returns an empty array.

bool sqliteCreateAggregate(string $function_name, callable $step_func, callable $finalize_func, int $num_args = -1)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo_sqlite >= 1.0.0)
Registers an aggregating User Defined Function for use in SQL statements

Parameters

string $function_name

The name of the function used in SQL statements.

callable $step_func

Callback function called for each row of the result set. Your PHP function should accumulate the result and store it in the aggregation context.

callable $finalize_func

Callback function to aggregate the "stepped" data from each row. Once all the rows have been processed, this function will be called and it should then take the data from the aggregation context and return the result. This callback function should return a type understood by SQLite (i.e. scalar type).

int $num_args

[optional]

Hint to the SQLite parser if the callback function accepts a predetermined number of arguments.

Return Value

bool

TRUE on success or FALSE on failure.

bool sqliteCreateCollation(string $name, callable $callback)

(PHP 5 >= 5.3.11, PHP 7)
Registers a User Defined Function for use as a collating function in SQL statements

Parameters

string $name

Name of the SQL collating function to be created or redefined.

callable $callback

The name of a PHP function or user-defined function to apply as a callback, defining the behavior of the collation. It should accept two strings and return as strcmp() does, i.e. it should return -1, 1, or 0 if the first string sorts before, sorts after, or is equal to the second.

Return Value

bool

TRUE on success or FALSE on failure.

bool sqliteCreateFunction(string $function_name, callable $callback, int $num_args = -1, int $flags = 0)

(PHP 5 >= 5.1.0, PHP 7, PECL pdo_sqlite >= 1.0.0)
Registers a User Defined Function for use in SQL statements

Parameters

string $function_name

The name of the function used in SQL statements.

callable $callback

Callback function to handle the defined SQL function.

int $num_args

[optional]

The number of arguments that the SQL function takes. If this parameter is -1, then the SQL function may take any number of arguments.

int $flags

[optional]

A bitwise conjunction of flags. Currently, only PDO::SQLITE_DETERMINISTIC is supported, which specifies that the function always returns the same result given the same inputs within a single SQL statement.

Return Value

bool

TRUE on success or FALSE on failure.

bool pgsqlCopyFromArray(string $tableName, array $rows, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from PHP array into table

Parameters

string $tableName

String containing table name

array $rows

Array of strings with fields separated by separator

string $separator

Separator used in rows array

string $nullAs

How to interpret null values

string|null $fields

List of fields to insert

Return Value

bool

TRUE on success or FALSE on failure.

bool pgsqlCopyFromFile(string $tableName, string $filename, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from file into table

Parameters

string $tableName

String containing table name

string $filename

Filename containing data to import

string $separator

Separator used in file specified by filename

string $nullAs

How to interpret null values

string|null $fields

List of fields to insert

Return Value

bool

TRUE on success or FALSE on failure.

array|false pgsqlCopyToArray(string $tableName, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from database table into PHP array

Parameters

string $tableName

String containing table name

string $separator

Separator used in rows

string $nullAs

How to interpret null values

string|null $fields

List of fields to insert

Return Value

array|false

returns an array of rows, or FALSE on failure.

bool pgsqlCopyToFile(string $tableName, string $filename, string $separator = "\t", string $nullAs = "\\\\N", string|null $fields = null)

(PHP 5 >= 5.3.3, PHP 7, PHP 8)
Copy data from table into file

Parameters

string $tableName

String containing table name

string $filename

Filename to export data

string $separator

Separator used in file specified by filename

string $nullAs

How to interpret null values

string|null $fields

List of fields to insert

Return Value

bool

TRUE on success or FALSE on failure.

string|false pgsqlLOBCreate()

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL pdo_pgsql >= 1.0.2)
Creates a new large object

Return Value

string|false

returns the OID of the newly created large object on success, or FALSE on failure.

resource|false pgsqlLOBOpen(string $oid, string $mode = "rb")

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL pdo_pgsql >= 1.0.2)
Opens an existing large object stream

Parameters

string $oid

A large object identifier.

string $mode

If mode is r, open the stream for reading. If mode is w, open the stream for writing.

Return Value

resource|false

returns a stream resource on success or FALSE on failure.

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL pdo_pgsql >= 1.0.2)
Deletes the large object

Parameters

string $oid

A large object identifier.

Return Value

bool

TRUE on success or FALSE on failure.

array|false pgsqlGetNotify(int $fetchMode = PDO::FETCH_DEFAULT, int $timeoutMilliseconds = 0)

(PHP 5 >= 5.6.0, PHP 7, PHP 8)
Get asynchronous notification

Parameters

int $fetchMode

The format the result set should be returned as, represented as a PDO::FETCH_* constant.

int $timeoutMilliseconds

The length of time to wait for a response, in milliseconds.

Return Value

array|false

if one or more notifications is pending, returns a single row, with fields message and pid, otherwise FALSE.

int pgsqlGetPid()

(PHP 5 >= 5.6.0, PHP 7, PHP 8)
Get the server PID

Return Value

int

The server's PID.

static PDO connect(string $dsn, string|null $username = null, string|null $password = null, array|null $options = null)

Since: 8.4

No description

Parameters

string $dsn
string|null $username
string|null $password
array|null $options

Return Value

PDO

int getWarningCount()

No description

Return Value

int