MySQLi
The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP scripting language to provide an interface with MySQL protocol compatible databases (MariaDB, MySQL, Percona Server, TiDB).[src] MySQLi is an improved version of the older PHP MySQL driver, offering various benefits.[src] The authors of the PHP scripting language recommend using MySQLi when dealing with MySQL server versions 4.1.3 and newer, since it takes advantage of new functionality.[src]
The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP scripting language to provide an interface with MySQL protocol compatible databases (MariaDB, MySQL, Percona Server, TiDB).[src] MySQLi is an improved version of the older PHP MySQL driver, offering various benefits.[src] The authors of the PHP scripting language recommend using MySQLi when dealing with MySQL server versions 4.1.3 and newer, since it takes advantage of new functionality.[src] ==Technical details== The PHP code consists of a core, with optional extensions to the core functionality.[src] PHP's MySQL-related extensions, such as the MySQLi extension, and the MySQL extension, are implemented using the PHP extension framework.[src] The MySQLi extension provides various benefits with respect to its predecessor, the most prominent of which (according to the PHP website ) are: An object-oriented interface Support for prepared statements Support for multiple statements Support for transactions Enhanced debugging support ==Comparison of features== Source: There are three main API options when considering connecting to a MySQL database server: PHP's MySQLi Extension PHP Data Objects (PDO) PHP's MySQL Extension ==Usage== Source: ===Dual interface=== The MySQLi extension features a dual interface: it supports both the procedural and object-oriented programming paradigms.[src] Users migrating from the old MySQL extension may prefer the familiarity of the procedural interface; in many cases, the function names differ only by prefix.[src] Some MySQLi functions take a connection handle as their first argument, whereas matching functions in the old MySQL interface took it as an optional last argument.[src] New and old native function calls ===Connections=== The MySQL server supports the use of various transport layers for connections.[src] Connections use TCP/IP, Unix domain sockets or Windows named pipes.[src] The hostname localhost has a special meaning: it is bound to the use of Unix domain sockets.[src] It is not possible to open a TCP/IP connection using the hostname localhost – instead 127.0.0.1 must be used.[src] Connection examples: connect_errno) { echo "Failed to connect to MySQL: (" .[src] $mysqli->connect_errno .[src] ") " .[src] $mysqli->connect_error; } echo $mysqli->host_info .[src] "\n"; $mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" .[src] $mysqli->connect_errno .[src] ") " .[src] $mysqli->connect_error; } echo $mysqli->host_info .[src] "\n"; ?> Output Localhost via UNIX socket 127.0.0.1 via TCP/IP ===Executing statements=== Statements can be executed with the mysqli_query(), mysqli_real_query() and mysqli_multi_query() functions.[src] The mysqli_query() function is the most common, and combines the executing statement with a buffered fetch of its result set, if any, in one call.[src] Calling mysqli_query() is identical to calling mysqli_real_query() followed by mysqli_store_result().[src] Example: connect_errno) { echo "Failed to connect to MySQL: (" .[src] $mysqli->connect_errno .[src] ") " .[src] $mysqli->connect_error; } if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)") || !$mysqli->query("INSERT INTO test(id) VALUES (1)")) { echo "Table creation failed: (" .[src] $mysqli->errno .[src] ") " .[src] $mysqli->error; } ?> connect_errno) { echo "Failed to connect to MySQL: (" .[src] $mysqli->connect_errno .[src] ") " .[src] $mysqli->connect_error; } if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)") || !$mysqli->query("INSERT INTO test(id) VALUES (1), (2), (3)")) { echo "Table creation failed: (" .[src] $mysqli->errno .[src] ") " .[src] $mysqli->error; } $res = $mysqli->query("SELECT id FROM test ORDER BY id ASC"); echo "Reverse order...\n"; for ($row_no = $res->num_rows - 1; $row_no >= 0; $row_no--) { $res->data_seek($row_no); $row = $res->fetch_assoc(); echo " id = " .[src] $row['id'] .[src] "\n"; } echo "Result set order...\n"; $res->data_seek(0); while ($row = $res->fetch_assoc()) { echo " id = " .[src] $row['id'] .[src] "\n"; } ?> The above example will output: Reverse order...[src] id = 3 id = 2 id = 1 Result set order...[src] id = 1 id = 2 id = 3 real_query("SELECT id FROM test ORDER BY id ASC"); $res = $mysqli->use_result(); echo "Result set order...\n"; while ($row = $res->fetch_assoc()) { echo " id = " .[src] $row['id'] .[src] "\n"; } ?> connect_errno) { echo "Failed to connect to MySQL: (" .[src] $mysqli->connect_errno .[src] ") " .[src] $mysqli->connect_error; } if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT, label CHAR(1))") || !$mysqli->query("INSERT INTO test(id, label) VALUES (1, 'a')")) { echo "Table creation failed: (" .[src] $mysqli->errno .[src] ") " .[src] $mysqli->error; } $res = $mysqli->query("SELECT id, label FROM test WHERE id = 1"); $row = $res->fetch_assoc(); printf("id = %s (%s)\n", $row['id'], gettype($row['id'])); printf("label = %s (%s)\n", $row['label'], gettype($row['label'])); ?> The above example will output: id = 1 (string) label = a (string) It is possible to convert integer and float columns back to PHP numbers by setting the MYSQLI_OPT_INT_AND_FLOAT_NATIVE connection option, if using the mysqlnd library.[src] If set, the mysqlnd library will check the result set meta data column types and convert numeric SQL columns to PHP numbers, if the PHP data type value range allows for it.[src] This way, for example, SQL INT columns are returned as integers.[src] Example: Native data types with mysqlnd and connection option options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1); $mysqli->real_connect("example.com", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" .[src] $mysqli->connect_errno .[src] ") " .[src] $mysqli->connect_error; } if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT, label CHAR(1))") || !$mysqli->query("INSERT INTO test(id, label) VALUES (1, 'a')")) { echo "Table creation failed: (" .[src] $mysqli->errno .[src] ") " .[src] $mysqli->error; } $res = $mysqli->query("SELECT id, label FROM test WHERE id = 1"); $row = $res->fetch_assoc(); printf("id = %s (%s)\n", $row['id'], gettype($row['id'])); printf("label = %s (%s)\n", $row['label'], gettype($row['label'])); ?> The above example will output: id = 1 (integer) label = a (string) ===Prepared statements=== The MySQL database supports prepared statements.[src] A prepared statement or a parameterized statement is used to execute the same statement repeatedly with high efficiency.[src] The prepared statement execution consists of two stages: prepare and execute.[src] At the prepare stage a statement template is sent to the database server.[src] The server performs a syntax check and initializes server internal resources for later use.[src] The MySQL server supports using anonymous, positional placeholders with ?.[src] ===Stored procedures=== The MySQL database supports stored procedures.[src] A stored procedure is a subroutine stored in the database catalog.[src] Applications can call and execute the stored procedure.[src] The CALL SQL statement is used to execute a stored procedure.[src] Stored procedures can have IN, INOUT and OUT parameters, depending on the MySQL version.[src] The MySQLi interface has no special notion for the different kinds of parameters.[src] Input parameters are provided with the CALL statement.[src] ===Multiple statements=== MySQL optionally allows having multiple statements in one statement string.[src] Sending multiple statements at once reduces client-server round trips but requires special handling.[src] Multiple statements or multi queries must be executed with mysqli_multi_query().[src] The individual statements of the statement string are separated by semicolons.[src] Then, all result sets returned by the executed statements must be fetched.[src] The MySQL server allows having statements that do return result sets and statements that do not return result sets in one multiple statement.[src] ===API support for transactions=== The MySQL server supports transactions depending on the storage engine used.[src] Since MySQL 5.5, the default storage engine is InnoDB.[src] InnoDB has full ACID transaction support.[src] Transactions can either be controlled using SQL or API calls.[src] It is recommended to use API calls for enabling and disabling the auto commit mode and for committing and rolling back transactions.[src] ==The MySQLi extension and persistent connections== Persistent connection support was introduced in PHP 5.3 for the MySQLi extension.[src] Support was already present in PDO MYSQL and ext/mysql.[src] The idea behind persistent connections is that a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times.[src] This reduces the overhead of creating fresh connections every time one is required, as unused connections are cached and ready to be reused.[src] Unlike the MySQL extension, MySQLi does not provide a separate function for opening persistent connections.[src] To open a persistent connection you must prepend p: to the hostname when connecting.[src] The problem with persistent connections is that they can be left in unpredictable states by clients.[src] For example, a table lock might be activated before a client terminates unexpectedly.[src] A new client process reusing this persistent connection will get the connection "as is".[src] Any cleanup would need to be done by the new client process before it could make good use of the persistent connection, increasing the burden on the programmer.[src] The persistent connection of the MySQLi extension however provides built-in cleanup handling code.[src] The cleanup carried out by MySQLi includes: Rollback active transactions Close and drop temporary tables Unlock tables Reset session variables Close prepared statements (always happens with PHP) Close handler Release locks acquired with GET_LOCK() This ensures that persistent connections are in a clean state on return from the connection pool, before the client process uses them.[src] The MySQLi extension does this cleanup by automatically calling the C-API function mysql_change_user().[src] The automatic cleanup feature has advantages and disadvantages though.[src] The advantage is that the developer no longer needs to worry about adding cleanup code, as it is called automatically.[src] However, the disadvantage is that the code could potentially be a little slower, as the code to perform the cleanup needs to run each time a connection is returned from the connection pool.[src] It is possible to switch off the automatic cleanup code, by compiling PHP with MYSQLI_NO_CHANGE_USER_ON_PCONNECT defined.[src] ==References== ==Further reading== MySQLi Prepared Statements Tutorial MySQLi documentation on PHP.net Emulate MySQL extension with MySQLi[src]
103 sentences · 103 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"MySQLi." encyclopegia.org, 2026, https://encyclopegia.org/wiki/mysqli. Accessed 04 December 2026.
Chicago
"MySQLi." encyclopegia.org. Accessed December 4, 2026. https://encyclopegia.org/wiki/mysqli.
APA
encyclopegia.org. (2026). MySQLi. Retrieved December 4, 2026, from https://encyclopegia.org/wiki/mysqli
Accessed December 4, 2026 · CC BY-SA 4.0 · citation page · JSON