LXIV. Improved MySQL Extension

Introduction

The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above. More information about the MySQL Database server can be found at http://www.mysql.com/

Documentation for MySQL can be found at http://www.mysql.com/documentation/.

Warning

This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.

Requirements

In order to have these functions available, you must compile PHP with support for the mysqli extension.

Note: The mysqli extension is designed to work with the version 4.1 or above of MySQL. For previous versions, please see the MySQL extension documentation.

Installation

To install the mysqli extension for PHP, use the --with-mysqli=mysql_config_path configuration option where mysql_config_path represents the location of the mysql_config program that comes with MySQL versions greater than 4.1. Also, disable the standard MySQL extension (which is enabled by default) by also using the --without-mysql configuration option. If you would like to install the standard mysql extension along with the mysqli extension, the bundled libmysql library that comes with PHP cannot be used. Instead, use the client libraries installed by MySQL with versions below 4.1. This will force PHP to use the client libraries installed by MySQL thus avoiding any conflicts.

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 1. MySQLi Configuration Options

NameDefaultChangeable
mysqli.max_links"-1"PHP_INI_SYSTEM
mysqli.default_portNULLPHP_INI_ALL
mysqli.default_socketNULLPHP_INI_ALL
mysqli.default_hostNULLPHP_INI_ALL
mysqli.default_userNULLPHP_INI_ALL
mysqli.default_pwNULLPHP_INI_ALL

For further details and definitions of the above PHP_INI_* constants, see the chapter on configuration changes.

Here's a short explanation of the configuration directives.

mysqli.max_links integer

The maximum number of MySQL connections per process, including persistent connections.

mysqli.default_port string

The default TCP port number to use when connecting to the database server if no other port is specified. If no default is specified, the port will be obtained from the MYSQL_TCP_PORT environment variable, the mysql-tcp entry in /etc/services or the compile-time MYSQL_PORT constant, in that order. Win32 will only use the MYSQL_PORT constant.

mysqli.default_socket string

The default socket name to use when connecting to a local database server if no other socket name is specified.

mysqli.default_host string

The default server host to use when connecting to the database server if no other host is specified. Doesn't apply in safe mode.

mysqli.default_user string

The default user name to use when connecting to the database server if no other name is specified. Doesn't apply in safe mode.

mysqli.default_password string

The default password to use when connecting to the database server if no other password is specified. Doesn't apply in safe mode.

Resource Types

mysqli_link

Represents a connection between PHP and a MySQL database.

mysqli_stmt

Represents a prepared statement.

mysqli_result

Represents the result set obtained from a query against the database.

Predefined Constants

Table 2. MySQLi Constants

NameDescription
MYSQLI_READ_DEFAULT_GROUP (integer)  
MYSQLI_READ_DEFAULT_FILE (integer)  
MYSQLI_OPT_CONNECT_TIMEOUT (integer)  
MYSQLI_OPT_LOCAL_INFILE (integer)  
MYSQLI_INIT_COMMAND (integer)  
MYSQLI_CLIENT_SSL (integer)  
MYSQLI_CLIENT_COMPRESS (integer)  
MYSQLI_CLIENT_INTERACTIVE (integer)  
MYSQLI_CLIENT_IGNORE_SPACE (integer)  
MYSQLI_CLIENT_NO_SCHEMA (integer)  
MYSQLI_CLIENT_MULTI_QUERIES (integer)  
MYSQLI_STORE_RESULT (integer)  
MYSQLI_USE_RESULT (integer)  
MYSQLI_ASSOC (integer)  
MYSQLI_NUM (integer)  
MYSQLI_BOTH (integer)  
MYSQLI_NOT_NULL_FLAG (integer)  
MYSQLI_PRI_KEY_FLAG (integer)  
MYSQLI_UNIQUE_KEY_FLAG (integer)  
MYSQLI_MULTIPLE_KEY_FLAG (integer)  
MYSQLI_BLOB_FLAG (integer)  
MYSQLI_UNSIGNED_FLAG (integer)  
MYSQLI_ZEROFILL_FLAG (integer)  
MYSQLI_AUTO_INCREMENT_FLAG (integer)  
MYSQLI_TIMESTAMP_FLAG (integer)  
MYSQLI_SET_FLAG (integer)  
MYSQLI_NUM_FLAG (integer)  
MYSQLI_PART_KEY_FLAG (integer)  
MYSQLI_GROUP_FLAG (integer)  
MYSQLI_TYPE_DECIMAL (integer)  
MYSQLI_TYPE_TINY (integer)  
MYSQLI_TYPE_SHORT (integer)  
MYSQLI_TYPE_LONG (integer)  
MYSQLI_TYPE_FLOAD (integer)  
MYSQLI_TYPE_DOUBLE (integer)  
MYSQLI_TYPE_NULL (integer)  
MYSQLI_TYPE_TIMESTAMP (integer)  
MYSQLI_TYPE_LONGLONG (integer)  
MYSQLI_TYPE_INT24 (integer)  
MYSQLI_TYPE_DATE (integer)  
MYSQLI_TYPE_TIME (integer)  
MYSQLI_TYPE_DATETIME (integer)  
MYSQLI_TYPE_YEAR (integer)  
MYSQLI_TYPE_NEWDATE (integer)  
MYSQLI_TYPE_ENUM (integer)  
MYSQLI_TYPE_SET (integer)  
MYSQLI_TYPE_TINY_BLOB (integer)  
MYSQLI_TYPE_MEDIUM_BLOB (integer)  
MYSQLI_TYPE_LONG_BLOB (integer)  
MYSQLI_TYPE_BLOB (integer)  
MYSQLI_TYPE_VAR_STRING (integer)  
MYSQLI_TYPE_STRING (integer)  
MYSQLI_TYPE_CHAR (integer)  
MYSQLI_TYPE_INTERVAL (integer)  
MYSQLI_TYPE_GEOMETRY (integer)  
MYSQLI_BIND_STRING (integer)  
MYSQLI_BIND_INT (integer)  
MYSQLI_BIND_DOUBLE (integer)  
MYSQLI_BIND_SEND_DATA (integer)  
MYSQLI_RPL_MASTER (integer)  
MYSQLI_RPL_SLAVE (integer)  
MYSQLI_RPL_ADMIN (integer)  
MYSQLI_NEED_DATA (integer)  
MYSQLI_NO_DATA (integer)  
MYSQLI_PR_REPORT_STDERR (integer)  
MYSQLI_PR_REPORT_PORT (integer)  
MYSQLI_PR_REPORT_FILE (integer)  
Table of Contents
mysqli_affected_rows -- Gets the number of affected rows in a previous MySQL operation
mysqli_autocommit -- Turns on or off auto-commiting database modifications
mysqli_bind_param -- Binds variables to a prepared statement as parameters
mysqli_bind_result -- Binds variables to a prepared statement for result storage
mysqli_change_user -- Changes the user of the specified database connection
mysqli_character_set_name -- Returns the default character set for the database connection
mysqli_close -- Closes a previously opened database connection
mysqli_commit -- Commits the current transaction
mysqli_connect -- Open a new connection to the MySQL server
mysqli_data_seek -- Adjusts the result pointer to an arbitary row in the result
mysqli_debug -- Performs debugging operations
mysqli_disable_reads_from_master -- 
mysqli_disable_rpl_parse -- 
mysqli_dump_debug_info -- Dump debugging information into the log
mysqli_enable_reads_from_master -- 
mysqli_enable_rpl_parse -- 
mysqli_errno -- Returns the error code for the most recent function call
mysqli_error -- Returns a string description of the last error
mysqli_execute -- Executes a prepared Query
mysqli_fetch_array -- Fetch a result row as an associative, a numeric array, or both.
mysqli_fetch_assoc -- Fetch a result row as an associative array
mysqli_fetch_field_direct --  Fetch meta-data for a single field
mysqli_fetch_field -- Returns the next field in the result set
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 -- Get a result row as an enumerated array
mysqli_fetch --  Fetch results from a prepared statement into the bound variables
mysqli_field_count -- Returns the number of columns for the most recent query
mysqli_field_seek --  Set result pointer to a specified field offset
mysqli_field_tell --  Get current field offset of a result pointer
mysqli_free_result -- Frees the memory associated with a result
mysqli_get_client_info -- Returns the MySQL client version as a string
mysqli_get_host_info -- Returns a string representing the type of connection used
mysqli_get_proto_info -- Returns the version of the MySQL protocol used
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 a resource 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_master_query --  Enforce execution of a query on the master in a master/slave setup
mysqli_num_fields --  Get the number of fields in a result
mysqli_num_rows --  Gets the number of rows in a result
mysqli_options -- set options
mysqli_param_count -- Returns the number of parameter for the given statement
mysqli_ping --  Ping a server connection, or reconnect if there is no connection
mysqli_prepare_result -- 
mysqli_prepare --  Prepare a SQL statement for execution
mysqli_profiler -- 
mysqli_query -- Performs a query on the database
mysqli_read_query_result -- 
mysqli_real_connect -- Opens a connection to a mysql server
mysqli_real_escape_string --  Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
mysqli_real_query -- Execute an SQL query
mysqli_reload -- 
mysqli_rollback -- 
mysqli_rpl_parse_enabled -- 
mysqli_rpl_probe -- 
mysqli_rpl_query_type -- 
mysqli_select_db -- Selects the default database for database queries
mysqli_send_long_data -- 
mysqli_send_query -- 
mysqli_slave_query --  Enforces execution of a query on a slave in a master/slave setup
mysqli_ssl_set -- 
mysqli_stat --  Gets the current system status
mysqli_stmt_affected_rows -- 
mysqli_stmt_close -- close statement
mysqli_stmt_errno -- 
mysqli_stmt_error -- 
mysqli_stmt_store_result -- 
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 thread safety is given or not
mysqli_use_result -- Initiate a result set retrieval
mysqli_warning_count --  Returns the number of warnings from the last query for the given link