PHP MySQL Functions

Introduction to PHP MySQL

MySQL functions allow you to access the MySQL database server.

Installation

In order to be able to use the functions of this class smoothly, you must add MySQL support when compiling PHP.

You can simply use the --with-mysql[=DIR] configuration option when compiling, where the optional [DIR] points to the MySQL installation directory.

Although this MySQL extension library is compatible with MySQL 4.1.0 and later versions, it does not support the additional features provided by these versions. To use these features, please use the MySQLi extension library.

If you need to install both the mysql extension library and the mysqli extension library at the same time, you must use the same client library to avoid any conflicts.

Installation on Linux system

PHP 4

The --with-mysql option is enabled by default. This default behavior can be disabled using the --without-mysql configuration option. If MySQL is enabled without specifying the installation directory, PHP will use the bound MySQL client connection library.

Users of other applications that use MySQL (such as auth-mysql) should not use the bound library but specify the MySQL installation directory, as follows: --with-mysql=/path/to/mysql. This will force PHP to use the client connection library installed with MySQL, thus avoiding any conflicts.

PHP 5+

MySQL is not enabled by default and there is no bound MySQL library. Use the --with-mysql[=DIR] configuration option to add MySQL support. You can from MySQL Download the header files and libraries.

Installation on Windows system

PHP 4

The PHP MySQL extension is compiled into PHP.

PHP 5+

MySQL is not enabled by default, so you must activate the php_mysql.dll dynamic link library in php.ini. In addition, PHP needs to access the MySQL client connection library. The PHP Windows distribution includes a libmysql.dll, and in order for PHP to communicate with MySQL, this file must be placed in the Windows system path PATH.

To activate any PHP extension library (e.g., php_mysql.dll), the PHP directive extension_dir must be set to the directory where the PHP extension library is located. An example value for extension_dir under PHP 5 is c:\php\ext.

Note:If an error similar to the following occurs when starting the web server: "Unable to load dynamic library './php_mysql.dll'", it is because the system cannot find php_mysql.dll and/or libmysql.dll.

Runtime Configuration

The behavior of MySQL functions is affected by the settings in php.ini.

MySQL Configuration Options:

Name Default Description Changeable
mysql.allow_persistent "1" Whether persistent connections to MySQL are allowed. PHP_INI_SYSTEM
mysql.max_persistent "-1" The maximum number of persistent connections per process. PHP_INI_SYSTEM
mysql.max_links "-1" The maximum number of connections per process, including persistent connections. PHP_INI_SYSTEM
mysql.trace_mode "0" Trace mode. Available since PHP 4.3.0. PHP_INI_ALL
mysql.default_port NULL Specify the default TCP port for connecting to the database. PHP_INI_ALL
mysql.default_socket NULL The default socket name. Available since PHP 4.0.1. PHP_INI_ALL
mysql.default_host NULL The default server address. Not applicable to SQL secure mode. PHP_INI_ALL
mysql.default_user NULL The default username used. Not applicable to SQL secure mode. PHP_INI_ALL
mysql.default_password NULL The default password used. Not applicable to SQL secure mode. PHP_INI_ALL
mysql.connect_timeout "60" Connection timeout in seconds. PHP_INI_ALL

Resource Type

Two types of resource types are used in the MySQL module. The first is the database connection handle, and the second is the result set returned by the SQL query.

PHP MySQL Functions

PHP:Indicates the earliest PHP version that supports this function.

function Description PHP
mysql_affected_rows() Get the number of rows affected by the previous MySQL operation. 3
mysql_change_user() Not recommended. Change the logged-in user in the active connection. 3
mysql_client_encoding() Returns the name of the character set of the current connection. 4
mysql_close() Close a non-persistent MySQL connection. 3
mysql_connect() Open a non-persistent MySQL connection. 3
mysql_create_db() Not recommended. Create a new MySQL database. Use mysql_query() instead. 3
mysql_data_seek() Move the record pointer. 3
mysql_db_name() Return the database name from the call to mysql_list_dbs(). 3
mysql_db_query()

Not recommended. Send a MySQL query.

Use mysql_select_db() and mysql_query() instead.

3
mysql_drop_db()

Not recommended. Drop (delete) a MySQL database.

Use mysql_query() instead.

3
mysql_errno() Returns the numeric code of the error message from the last MySQL operation. 3
mysql_error() Returns the text error message produced by the last MySQL operation. 3
mysql_escape_string()

Not recommended. Escape a string for use in mysql_query.

Use mysql_real_escape_string() instead.

4
mysql_fetch_array() Get a row from the result set as an associative array, or a numeric array, or both. 3
mysql_fetch_assoc() Get a row from the result set as an associative array. 4
mysql_fetch_field() Get column information from the result set and return it as an object. 3
mysql_fetch_lengths() Get the length of the content of each field in the result set. 3
mysql_fetch_object() Get a row from the result set as an object. 3
mysql_fetch_row() Get a row from the result set as a numeric array. 3
mysql_field_flags() Get the flags associated with the specified field from the result. 3
mysql_field_len() Returns the length of the specified field. 3
mysql_field_name() Get the field name of the specified field in the result. 3
mysql_field_seek() Set the pointer in the result set to the specified field offset. 3
mysql_field_table() Get the table name where the specified field is located. 3
mysql_field_type() Get the type of the specified field in the result set. 3
mysql_free_result() Free the result memory. 3
mysql_get_client_info() Get MySQL client information. 4
mysql_get_host_info() Get MySQL host information. 4
mysql_get_proto_info() Get MySQL protocol information. 4
mysql_get_server_info() Get MySQL server information. 4
mysql_info() Get information about the latest query. 4
mysql_insert_id() Get the ID generated by the previous INSERT operation. 3
mysql_list_dbs() List all databases in the MySQL server. 3
mysql_list_fields()

Disapproved. List fields in the MySQL result.

Use mysql_query() instead.

3
mysql_list_processes() List MySQL processes. 4
mysql_list_tables()

Disapproved. List tables in the MySQL database.

Use mysql_query() instead.

3
mysql_num_fields() Get the number of fields in the result set. 3
mysql_num_rows() Get the number of rows in the result set. 3
mysql_pconnect() Open a persistent connection to the MySQL server. 3
mysql_ping() Ping a server connection, reconnect if no connection is present. 4
mysql_query() Send a MySQL query. 3
mysql_real_escape_string() Escape special characters in the string used in SQL statements. 4
mysql_result() Get the result data. 3
mysql_select_db() Select the MySQL database. 3
mysql_stat() Get the current system status. 4
mysql_tablename() Disapproved. Get the table name. Use mysql_query() instead. 3
mysql_thread_id() Return the ID of the current thread. 4
mysql_unbuffered_query() Send an SQL query to MySQL (do not retrieve / cache the result). 4

PHP MySQL Constants

In versions of PHP 4.3.0 and later, more client flags can be specified in the mysql_connect() function and mysql_pconnect() function:

PHP:Indicates the earliest PHP version that supports this constant.

Constants Description PHP
MYSQL_CLIENT_COMPRESS Use compressed communication protocol. 4.3
MYSQL_CLIENT_IGNORE_SPACE Allow spaces after the function name. 4.3
MYSQL_CLIENT_INTERACTIVE The inactive timeout allowed before closing the connection. 4.3
MYSQL_CLIENT_SSL Use SSL encryption (available only when the MySQL client library version is 4+). 4.3

The mysql_fetch_array() function uses a constant to represent the type of the returned array:

Constants Description PHP
MYSQL_ASSOC The returned data columns use field names as array index names.  
MYSQL_BOTH The returned data columns use field names and numeric indices as array index names.  
MYSQL_NUM

The returned data columns use numeric indices as array index names.

The index starts from 0, indicating the first field returned.