PHP MySQLi Functions

Beginner
5 min read
Updated: Nov 2026
77 function guides
Function index

About This Index

This page is the PHP MySQLi Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.

Learn how connect, charset, query vs prepare, fetch, and close fit together on /php/mysqli. Use this page when you need a specific function such as mysqli_connect(), mysqli_query(), or mysqli_fetch_assoc().

Beginner path.

Start with mysqli_connect() and mysqli_set_charset()—then learn mysqli_query(), mysqli_fetch_assoc(), and mysqli_prepare(), and always check mysqli_connect_error() / mysqli_errno().

Aliases and newer APIs.

Alias pages are included so the sidebar list is complete (escape_string → real_escape_string, set_opt → options). Prefer the primary name in new code. mysqli_execute_query() (PHP 8.2) and mysqli_fetch_column() (PHP 8.1) are marked in the tables below.

How to Call a PHP MySQLi Function

Procedural MySQLi helpers are global functions—pass the link (or result) as the first argument. Set charset after connect; close when you are done:

PHP
$link = mysqli_connect('127.0.0.1', 'user', 'pass', 'db');
if (mysqli_connect_errno()) {
    die(mysqli_connect_error());
}
mysqli_set_charset($link, 'utf8mb4');

$result = mysqli_query($link, 'SELECT id, name FROM users LIMIT 5');
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'], PHP_EOL;
}

// Prefer prepare() for user values
// $stmt = mysqli_prepare($link, 'SELECT name FROM users WHERE id = ?');

mysqli_close($link);

PHP MySQLi Functions Table

All function guides in this PHP tutorial series (77 tutorials in 8 categories).

Connect & options

11 functions

Open a connection, set options or SSL before connect, switch user or database, set charset, ping the server, and close when you are done.

FunctionDescriptionTutorial
mysqli_change_user()Change the user of an open connection.Try it
mysqli_close()Close a previously opened database connection.Try it
mysqli_connect()Open a new connection to the MySQL server.Try it
mysqli_init()Initialize MySQLi and return a handle for real_connect().Try it
mysqli_options()Set options before real_connect().Try it
mysqli_ping()Ping the server connection, or reconnect if needed.Try it
mysqli_real_connect()Open a connection using an init() handle and options.Try it
mysqli_select_db()Select the default database for queries.Try it
mysqli_set_charset()Set the default client character set (e.g. utf8mb4).Try it
mysqli_set_opt()Alias of mysqli_options(): set connection options.Try it
mysqli_ssl_set()Set SSL parameters used by real_connect().Try it

Errors & info

10 functions

Read connect and query errors, SQLSTATE, warnings, and last-query info strings from the server.

FunctionDescriptionTutorial
mysqli_connect_errno()Return the error code from the last connect call.Try it
mysqli_connect_error()Return the error message from the last connect call.Try it
mysqli_errno()Return the error code for the most recent function call.Try it
mysqli_error()Return the error message for the most recent function call.Try it
mysqli_error_list()Return a list of errors from the last command.Try it
mysqli_get_warnings()Get the result set of SHOW WARNINGS.Try it
mysqli_info()Return information about the last query.Try it
mysqli_sqlstate()Return the SQLSTATE error code from the last error.Try it
mysqli_stat()Return the current system status string.Try it
mysqli_warning_count()Return the number of warnings from the last query.Try it

Query & results

18 functions

Run SQL with query, real_query, multi_query, or prepare; store or use results; track affected rows, insert id, and field metadata.

FunctionDescriptionTutorial
mysqli_affected_rows()Get the number of affected rows from the last query.Try it
mysqli_current_field()Get the current field offset of a result pointer.Try it
mysqli_data_seek()Move the result pointer to an arbitrary row number.Try it
mysqli_execute_query()Prepare, bind, and execute a query in one call (PHP 8.2).Try it
mysqli_field_count()Get the number of columns for the most recent query.Try it
mysqli_field_seek()Set the field cursor to a given offset.Try it
mysqli_free()Free memory associated with a result.Try it
mysqli_insert_id()Return the auto-generated id from the last INSERT.Try it
mysqli_more_results()Check whether more results exist from multi_query().Try it
mysqli_multi_query()Perform one or more queries against the database.Try it
mysqli_next_result()Prepare the next result set from multi_query().Try it
mysqli_num_rows()Get the number of rows in a result.Try it
mysqli_prepare()Prepare an SQL statement for execution.Try it
mysqli_query()Perform a query on the database.Try it
mysqli_real_query()Execute a query; pair with store_result() or use_result().Try it
mysqli_stmt_init()Initialize a statement object for prepare().Try it
mysqli_store_result()Transfer a result set from the last query.Try it
mysqli_use_result()Initiate a result set retrieval without buffering all rows.Try it

Fetch

10 functions

Read rows and field metadata from a result: associative arrays, numeric rows, objects, one column, or field info.

FunctionDescriptionTutorial
mysqli_fetch_all()Fetch all result rows as an array.Try it
mysqli_fetch_array()Fetch a result row as an associative, numeric, or both.Try it
mysqli_fetch_assoc()Fetch a result row as an associative array.Try it
mysqli_fetch_column()Fetch a single column from the next row (PHP 8.1).Try it
mysqli_fetch_field()Return the next field in the result set.Try it
mysqli_fetch_field_direct()Fetch metadata for a single field as an object.Try it
mysqli_fetch_fields()Return an array of objects representing the fields.Try it
mysqli_fetch_lengths()Return the lengths of the columns of the current row.Try it
mysqli_fetch_object()Fetch a result row as an object.Try it
mysqli_fetch_row()Fetch a result row as an enumerated array.Try it

Transactions

6 functions

Turn autocommit on or off, begin a transaction, commit or roll back, and manage savepoints.

FunctionDescriptionTutorial
mysqli_autocommit()Turn on or off auto-committing database modifications.Try it
mysqli_begin_transaction()Start a transaction.Try it
mysqli_commit()Commit the current transaction.Try it
mysqli_release_savepoint()Remove a named savepoint from the current transaction.Try it
mysqli_rollback()Roll back the current transaction.Try it
mysqli_savepoint()Set a named transaction savepoint.Try it

Escape & charset

5 functions

Escape strings for legacy SQL building, and inspect the connection character set. Prefer prepare() for user input.

FunctionDescriptionTutorial
mysqli_character_set_name()Return the current character set of the connection.Try it
mysqli_escape_string()Alias of mysqli_real_escape_string().Try it
mysqli_get_charset()Return a character set object for the connection.Try it
mysqli_quote_string()Escape and quote a string for use in SQL.Try it
mysqli_real_escape_string()Escape special characters in a string for SQL.Try it

Client / server info & stats

11 functions

Read client and server version strings, host and protocol info, connection stats, and thread helpers.

FunctionDescriptionTutorial
mysqli_get_client_info()Return the MySQL client library version as a string.Try it
mysqli_get_client_stats()Return client per-process statistics.Try it
mysqli_get_client_version()Return the MySQL client version as an integer.Try it
mysqli_get_connection_stats()Return statistics about the client connection.Try it
mysqli_get_host_info()Return a string describing the connection type.Try it
mysqli_get_links_stats()Return information about open and cached links.Try it
mysqli_get_proto_info()Return the version of the MySQL protocol used.Try it
mysqli_get_server_info()Return the MySQL server version as a string.Try it
mysqli_get_server_version()Return the MySQL server version as an integer.Try it
mysqli_thread_id()Return the thread ID for the current connection.Try it
mysqli_thread_safe()Return whether the client library is thread-safe.Try it

Async & advanced

6 functions

Poll asynchronous queries, reap results, kill threads, refresh server caches, and dump debug information.

FunctionDescriptionTutorial
mysqli_debug()Perform debugging operations.Try it
mysqli_dump_debug_info()Dump debugging information into the log.Try it
mysqli_kill()Ask the server to kill a MySQL thread.Try it
mysqli_poll()Poll connections for asynchronous query completion.Try it
mysqli_reap_async_query()Get the result from an asynchronous query.Try it
mysqli_refresh()Flush tables or caches on the server.Try it

Frequently Asked Questions

They are the built-in MySQLi helpers for connecting to MySQL or MariaDB, setting options and charset, running queries or prepared statements, fetching rows, handling errors and transactions, escaping strings, and reading client/server stats. This index links to a full tutorial for each one.
The MySQLi page teaches the ideas: connect, set_charset, query vs prepare, fetch_assoc loops, connect_error/errno, and close, with three fixed examples. This index lists every function tutorial so you can open mysqli_connect(), mysqli_query(), mysqli_prepare(), and the rest.
Start with mysqli_connect() and mysqli_set_charset(), then mysqli_query() with mysqli_fetch_assoc(), then mysqli_prepare() for user input. Always check mysqli_connect_error() or mysqli_errno(). Alias pages such as mysqli_escape_string() teach the same idea as their primary names.
mysqli_escape_string() is an alias of mysqli_real_escape_string(). mysqli_set_opt() is an alias of mysqli_options(). Prefer the primary name in new code, and prefer prepared statements over escaping when values come from the user.
mysqli_fetch_column() (PHP 8.1) returns a single column from the next row. mysqli_execute_query() (PHP 8.2) prepares, binds, and executes in one call and returns a result. Confirm function_exists() if you support older PHP versions.
Open mysqli_affected_rows() next (it is the first guide in sidebar order), or jump to mysqli_connect() and mysqli_query() if you are just starting. Read PHP MySQLi first if the groups below still feel unfamiliar.

Start with mysqli_affected_rows()

Open the next guide—syntax, examples, and FAQs.

mysqli_affected_rows() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful