MySQLi Introduction
The MySQLi functions allows you to access MySQL database servers. Note: The MySQLi extension is designed to work with MySQL version 4.1.13 or newer.Installation / Runtime Configuration
For the MySQLi functions to be available, you must compile PHP with support for the MySQLi extension. The MySQLi extension was introduced with PHP version 5.0.0. The MySQL Native Driver was included in PHP version 5.3.0. For installation details, go to: http://php.net/manual/en/mysqli.installation.php For runtime configuration details, go to: http://php.net/manual/en/mysqli.configuration.phpMySQLi Functions
Function | Description |
---|---|
affected_rows() | Returns the number of affected rows in the previous MySQL operation |
autocommit() | Turns on or off auto-committing database modifications |
begin_transaction() | Starts a transaction |
change_user() | Changes the user of the specified database connection |
character_set_name() | Returns the default character set for the database connection |
close() | Closes a previously opened database connection |
commit() | Commits the current transaction |
connect() | Opens a new connection to the MySQL server |
connect_errno() | Returns the error code from the last connection error |
connect_error() | Returns the error description from the last connection error |
data_seek() | Adjusts the result pointer to an arbitrary row in the result-set |
debug() | Performs debugging operations |
dump_debug_info() | Dumps debugging info into the log |
errno() | Returns the last error code for the most recent function call |
error() | Returns the last error description for the most recent function call |
error_list() | Returns a list of errors for the most recent function call |
fetch_all() | Fetches all result rows as an associative array, a numeric array, or both |
fetch_array() | Fetches a result row as an associative, a numeric array, or both |
fetch_assoc() | Fetches a result row as an associative array |
fetch_field() | Returns the next field in the result-set, as an object |
fetch_field_direct() | Returns meta-data for a single field in the result-set, as an object |
fetch_fields() | Returns an array of objects that represent the fields in a result-set |
fetch_lengths() | Returns the lengths of the columns of the current row in the result-set |
fetch_object() | Returns the current row of a result-set, as an object |
fetch_row() | Fetches one row from a result-set and returns it as an enumerated array |
field_count() | Returns the number of columns for the most recent query |
field_seek() | Sets the field cursor to the given field offset |
get_charset() | Returns a character set object |
get_client_info() | Returns the MySQL client library version |
get_client_stats() | Returns statistics about client per-process |
get_client_version() | Returns the MySQL client library version as an integer |
get_connection_stats() | Returns statistics about the client connection |
get_host_info() | Returns the MySQL server hostname and the connection type |
get_proto_info() | Returns the MySQL protocol version |
get_server_info() | Returns the MySQL server version |
get_server_version() | Returns the MySQL server version as an integer |
info() | Returns information about the last executed query |
init() | Initializes MySQLi and returns a resource for use with real_connect() |
insert_id() | Returns the auto-generated id from the last query |
kill() | Asks the server to kill a MySQL thread |
more_results() | Checks if there are more results from a multi query |
multi_query() | Performs one or more queries on the database |
next_result() | Prepares the next result-set from multi_query() |
options() | Sets extra connect options and affect behavior for a connection |
ping() | Pings a server connection, or tries to reconnect if the connection has gone down |
poll() | Polls connections |
prepare() | Prepares an SQL statement for execution |
query() | Performs a query against a database |
real_connect() | Opens a new connection to the MySQL server |
real_escape_string() | Escapes special characters in a string for use in an SQL statement |
real_query() | Executes a single SQL query |
reap_async_query() | Returns result from an async SQL query |
refresh() | Refreshes/flushes tables or caches, or resets the replication server information |
rollback() | Rolls back the current transaction for the database |
select_db() | Select the default database for database queries |
set_charset() | Sets the default client character set |
set_local_infile_default() | Unsets user defined handler for load local infile command |
set_local_infile_handler() | Set callback function for LOAD DATA LOCAL INFILE command |
sqlstate() | Returns the SQLSTATE error code for the error |
ssl_set() | Used to establish secure connections using SSL |
stat() | Returns the current system status |
stmt_init() | Initializes a statement and returns an object for use with stmt_prepare() |
store_result() | Transfers a result-set from the last query |
thread_id() | Returns the thread ID for the current connection |
thread_safe() | Returns whether the client library is compiled as thread-safe |
use_result() | Initiates the retrieval of a result-set from the last query executed |
warning_count() | Returns the number of warnings from the last query in the connection |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | The number of rows affected. -1 indicates that the query returned an error |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
mode | Required. FALSE turns auto-commit off. TRUE turns auto-commit on (and commits any waiting queries) |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
username | Required. Specifies the MySQL user name |
password | Required. Specifies the MySQL password |
dbname | Required. Specifies the database to change to |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | The default character set for the specified connection |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to close |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
flags | Optional. A constant:
|
name | Optional. COMMIT/*name*/ is executed if this parameter is specified |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
PHP Changelog: | PHP 5.5: Added the flags and name parameters |
Parameter | Description |
---|---|
host | Optional. Specifies a host name or an IP address |
username | Optional. Specifies the MySQL username |
password | Optional. Specifies the MySQL password |
dbname | Optional. Specifies the default database to be used |
port | Optional. Specifies the port number to attempt to connect to the MySQL server |
socket | Optional. Specifies the socket or named pipe to be used |
Return Value: | Returns an object representing the connection to the MySQL server |
---|---|
PHP Version: | 5+ |
Return Value: | Returns an error code value. Zero if no error occurred |
---|---|
PHP Version: | 5+ |
Return Value: | Returns a string that describes the error. NULL if no error occurred |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
offset | Required. Specifies the field offset. Must be between 0 and the total number of rows - 1 |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
message | Required. A string that represents the debugging operation to perform |
Return Value: | TRUE |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
link | Required. A link identifier returned by connect() or init() |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an error code value. Zero if no error occurred |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns a string with the error description. " if no error occurred |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns a list of errors as an associative array; with errno (error code), error (error text), and sqlstate |
---|---|
PHP Version: | 5.4+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
resulttype | Optional. Specifies what type of array that should be produced. Can be one of the following values:
|
Return Value: | Returns an array of associative or numeric arrays holding the result rows |
---|---|
PHP Version: | 5.3+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
resulttype | Optional. Specifies what type of array that should be produced. Can be one of the following values:
|
Return Value: | Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result-set |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
Return Value: | Returns an associative array of strings representing the fetched row. NULL if there are no more rows in result-set |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
Return Value: | Returns an object containing field definition information. FALSE if no info is available. The object has the following properties:
|
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
fieldnr | Required. Specifies the field number. Must be an integer from 0 to number of fields-1 |
Return Value: | Returns an object containing field definition information. FALSE if no info is available. The object has the following properties:
|
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
Return Value: | Returns an array of objects containing field definition information. FALSE if no info is available. The objects have the following properties:
|
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
Return Value: | Returns an array of integers that represents the size of each field (column). FALSE if an error occurs |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
classname | Optional. Specifies the name of the class to instantiate, set the properties of, and return |
params | Optional. Specifies an array of parameters to pass to the constructor for classname objects |
Return Value: | Returns an object with string properties for the fetched row. NULL if there are no more rows in the result set |
---|---|
PHP Version: | 5+ |
Changelog: | The ability to return as a different object was added in PHP 5.0.0 |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
Return Value: | Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result set |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an integer that represents the number of columns in the result set |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
result | Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
fieldnr | Required. Specifies the field number. Must be an integer from 0 to number of fields-1 |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns a character set object with the following properties:
|
---|---|
PHP Version: | 5.1+ |
Parameter | Description |
---|---|
connection | Optional. Specifies the MySQL connection to use |
Return Value: | Returns a string that represents the MySQL client library version |
---|---|
PHP Version: | 5+ |
Return Value: | An array with client stats on success. FALSE on failure |
---|---|
PHP Version: | 5.3+ |
Parameter | Description |
---|---|
connection | Optional. Specifies the MySQL connection to use |
Return Value: | Returns an integer that represents the MySQL client version |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an array with connection stats on success. FALSE on failure |
---|---|
PHP Version: | 5.3+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns a string that represents the MySQL server hostname and the connection type |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an integer that represents the MySQL protocol version |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | A string that represents the MySQL server version |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an integer that represents the MySQL server version |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | A string that contains additional info about the last executed query |
---|---|
PHP Version: | 5+ |
Return Value: | An object to use with the mysqli_real_connect() function |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | An integer that represents the value of the AUTO_INCREMENT field updated by the last query. Returns zero if there were no update or no AUTO_INCREMENT field |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
processid | Required. The thread ID returned from thread_id() |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | TRUE if there is one or more result sets available from multi_query(). FALSE otherwise |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies one or more queries, separated with semicolon |
Return Value: | FALSE if the first query fails |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
option | Required. Specifies the option to set. Can be one of the following values:
|
value | Required. Specifies the value for the option |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
PHP Changelog: | PHP 5.5: Added MYSQLI_SERVER_PUBLIC_KEY option PHP 5.3: Added MYSQLI_OPT_INT_AND_FLOAT_NATIVE, MYSQLI_OPT_NET_CMD_BUFFER_SIZE, MYSQLI_OPT_NET_READ_BUFFER_SIZE, and MYSQLI_OPT_SSL_VERIFY_SERVER_CERT options |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
read | Required. Specifies a list of connections to check for outstanding results that can be read |
error | Required. Specifies a list of connections on which an error occurred, like query failure or lost connection |
reject | Required. Specifies a list of connections rejected because no asynchronous query has been run on for which the function could poll results |
seconds | Required. Specifies the maximum number of seconds to wait |
microseconds | Optional. Specifies the maximum number of microseconds to wait. Default is 0 |
Return Value: | The number of ready connections on success. FALSE on failure |
---|---|
PHP Version: | 5.3+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies an SQL query. Note: Do not add semicolon to the end of the query! |
Return Value: | A statement object on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies the SQL query string |
resultmode |
Optional. A constant. Can be one of the following:
|
Return Value: | For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failure |
---|---|
PHP Version: | 5+ |
PHP Changelog: | PHP 5.3.0 added the ability for async queries |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
host | Optional. Specifies a host name or an IP address |
username | Optional. Specifies the MySQL username |
password | Optional. Specifies the MySQL password |
dbname | Optional. Specifies the default database to be used |
port | Optional. Specifies the port number to attempt to connect to the MySQL server |
socket | Optional. Specifies the socket or named pipe to be used |
flag | Optional. Specifies different connection options. Possible values:
|
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
PHP Changelog: | PHP 5.6: Added MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT flag |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
escapestring | Required. The string to be escaped. Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z. |
Return Value: | Returns the escaped string |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. The query to be executed |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | mysqli_result on success. FALSE on failure |
---|---|
PHP Version: | 5.3+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
options | The options to refresh. Can be one of more of the following (separated by OR):
|
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5.3+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
flags | Optional. A constant:
|
name | Optional. ROLLBACK/*name*/ is executed if this parameter is specified |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
PHP Changelog: | PHP 5.5: Added the flags and name parameters |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
name | Required. Specifies the database name |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
charset | Required. Specifies the default character set |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5.0.5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
read_func | Required. Specifies a callback function or objext that can take the following params: stream - A PHP stream associated with the SQL commands INFILE &buffer - A string buffer to store the rewritten input into buflen - The maximum number of characters to be stored in the buffer &erromsg - If an error occurs you can store an error message in here |
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | A string containing the SQLSTATE error code for the last error |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
key | Required. Specifies the path name to the key file |
cert | Required. Specifies the path name to the certificate file |
ca | Required. Specifies the path name to the certificate authority file |
capath | Required. Specifies the pathname to a directory that contains trusted SSL CA certificates in PEM format |
cipher | Required. Specifies a list of allowable ciphers to use for SSL encryption |
Return Value: | Always TRUE. If SSL setup is incorrect, real_connect() will return an error when you try to connect |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | A string that describes the server status. FALSE on error |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an object |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns the thread ID for the current connection |
---|---|
PHP Version: | 5+ |
Return Value: | TRUE if the client library is thread-safe. FALSE otherwise |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | Returns an unbuffered result object. FALSE on error |
---|---|
PHP Version: | 5+ |
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Return Value: | The number of warnings from the last query. 0 if no warnings |
---|---|
PHP Version: | 5+ |