Skip to content

php_ref_mysqli

MySQLi stands for MySQL Improved. It is a PHP extension developed to take advantage of features found in MySQL versions 4.1.3 and newer. It provides an interface for interacting with MySQL databases from PHP.

MySQLi offers both a procedural interface (similar to the older, deprecated mysql_* functions) and an object-oriented interface. The object-oriented approach is generally preferred for modern PHP development.

Key features include support for prepared statements, transactions, multiple statements, and enhanced debugging capabilities.

PDO (PHP Data Objects) is another popular extension for database access, offering a consistent interface across different database systems. Choosing between MySQLi and PDO depends on project requirements (MySQLi is specific to MySQL/MariaDB, PDO is more portable).

The MySQLi extension is typically included and enabled by default in most modern PHP installations, especially when compiled with the MySQL Native Driver (mysqlnd). mysqlnd is the recommended library as it offers better performance and features compared to the older libmysqlclient.

You can verify if MySQLi is enabled by checking your phpinfo() output or using extension_loaded('mysqli').

For detailed installation and configuration options (e.g., specific socket paths, default ports), refer to the official PHP manual:

Installation: https://www.php.net/manual/en/mysqli.installation.php

Runtime Configuration: https://www.php.net/manual/en/mysqli.configuration.php

PHP MySQLi Functions (Procedural Interface)

Section titled “PHP MySQLi Functions (Procedural Interface)”

Below is a list of commonly used functions from the procedural interface. Most have object-oriented equivalents (e.g., mysqli_connect() vs $mysqli->connect()).

FunctionDescription
mysqli_affected_rows()Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query.
mysqli_autocommit()Turns on or off auto-committing database modifications.
mysqli_change_user()Changes the user of the specified database connection.
mysqli_character_set_name()Returns the default character set name for the database connection.
mysqli_close()Closes a previously opened database connection.
mysqli_commit()Commits the current transaction.
mysqli_connect_errno()Returns the error code from the last connection attempt.
mysqli_connect_error()Returns a string description of the last connection error.
mysqli_connect()Opens a new connection to the MySQL server.
mysqli_data_seek()Adjusts the result pointer to an arbitrary row in a result set.
mysqli_debug()Performs debugging operations (requires special PHP build).
mysqli_dump_debug_info()Dumps debugging information into the log.
mysqli_errno()Returns the error code for the most recent function call.
mysqli_error_list()Returns a list of errors from the last command executed.
mysqli_error()Returns a string description of the last error.
mysqli_fetch_all()Fetches all result rows as an array (Requires mysqlnd).
mysqli_fetch_array()Fetches a result row as an associative array, a numeric array, or both.
mysqli_fetch_assoc()Fetches a result row as an associative array.
mysqli_fetch_field_direct()Fetches metadata for a single field by offset.
mysqli_fetch_field()Returns the next field in the result set as an object.
mysqli_fetch_fields()Returns an array of objects representing the fields in a result set.
mysqli_fetch_lengths()Returns the lengths of the columns of the current row in the result set.
mysqli_fetch_object()Returns the current row of a result set as an object.
mysqli_fetch_row()Fetches one row of data from the result set and returns it as an enumerated array.
mysqli_field_count()Returns the number of columns for the most recent query.
mysqli_field_seek()Sets the field cursor to the given field offset.
mysqli_field_tell()Returns the position of the field cursor.
mysqli_free_result()Frees the memory associated with a result set.
mysqli_get_charset()Returns a character set object.
mysqli_get_client_info()Returns the MySQL client library version as a string.
mysqli_get_client_stats()Returns client per-process statistics (Requires mysqlnd).
mysqli_get_client_version()Returns the MySQL client library version as an integer.
mysqli_get_connection_stats()Returns statistics about the client connection (Requires mysqlnd).
mysqli_get_host_info()Returns a string describing the connection.
mysqli_get_proto_info()Returns the MySQL protocol version used by the connection.
mysqli_get_server_info()Returns the version of the MySQL server.
mysqli_get_server_version()Returns the version of the MySQL server as an integer.
mysqli_info()Retrieves information about the most recently executed query.
mysqli_init()Initializes MySQLi and returns an object for use with mysqli_real_connect().
mysqli_insert_id()Returns the auto-generated ID used in the last query.
mysqli_kill()Asks the server to kill a MySQL thread.
mysqli_more_results()Checks if there are more results from a multi query.
mysqli_multi_query()Performs one or more queries on the database.
mysqli_next_result()Prepares the next result set from mysqli_multi_query().
mysqli_num_fields()Returns the number of fields in a result set.
mysqli_num_rows()Returns the number of rows in a result set.
mysqli_options()Sets connection options (like MYSQLI_OPT_CONNECT_TIMEOUT).
mysqli_ping()Pings a server connection, or tries to reconnect if the connection has gone down.
mysqli_prepare()Prepares an SQL statement for execution (for Prepared Statements).
mysqli_query()Performs a query against the database.
mysqli_real_connect()Opens a connection to a mysql server (often used after mysqli_init()).
mysqli_real_escape_string()Escapes special characters in a string for use in an SQL statement (Use prepared statements instead!).
mysqli_real_query()Executes an SQL query.
mysqli_reap_async_query()Returns the result from an asynchronous query (Requires mysqlnd).
mysqli_refresh()Refreshes tables or caches, or resets replication server information.
mysqli_rollback()Rolls back the current transaction.
mysqli_select_db()Selects the default database for database queries (Deprecated; specify in connection or use object property).
mysqli_set_charset()Sets the default client character set.
mysqli_set_local_infile_default()Unsets a user-defined handler for LOAD DATA LOCAL INFILE.
mysqli_set_local_infile_handler()Sets a callback function for the LOAD DATA LOCAL INFILE command.
mysqli_sqlstate()Returns the SQLSTATE error code for the last MySQL operation.
mysqli_ssl_set()Used for establishing secure connections using SSL.
mysqli_stat()Returns the current system status.
mysqli_stmt_init()Initializes a statement and returns an object for use with mysqli_stmt_prepare().
mysqli_store_result()Transfers a result set from the last query.
mysqli_thread_id()Returns the thread ID for the current connection.
mysqli_thread_safe()Returns whether the client library is compiled as thread-safe.
mysqli_use_result()Initiates the retrieval of a result set without storing it locally (use with caution).
mysqli_warning_count()Returns the number of warnings from the last query.

Refer to the official PHP documentation for the most up-to-date information, parameter details, return values, and examples for each function and its object-oriented counterpart:

https://www.php.net/manual/en/ref.mysqli.php