0
PHP MySQL Functions
Posted by jujur
on
5:51 AM
PHP MySQL Functions
PHP MySQL Introduction
The MySQL functions allows you to
access MySQL database servers.
Installation
For the MySQL functions to be
available, you must compile PHP with MySQL support.For compiling, use --with-mysql=DIR (the optional DIR points to the MySQL directory).
Note: For full functionality of MySQL versions greater than 4.1., use the MySQLi extension instead. If you would like to install both the mysql extension and the mysqli extension you should use the same client library to avoid any conflicts.
Installation on Linux Systems
PHP 5+: MySQL and the MySQL library is not enabled by default. Use the --with-mysql=DIR configure option to include MySQL support and download headers and libraries from http://www.mysql.com/.
Installation on Windows Systems
PHP 5+: MySQL is not enabled by default, so the php_mysql.dll must be enabled inside of php.ini. Also, PHP needs access to the MySQL client library. A file named libmysql.dll is included in the Windows PHP distribution, and in order for PHP to talk to MySQL this file needs to be available to the Windows systems PATH.
To enable any PHP extension, the PHP extension_dir setting (in the php.ini file) should be set to the directory where the PHP extensions are located. An example extension_dir value is c:\php\ext.
Note: If you get the following error when starting the web server: "Unable to load dynamic library './php_mysql.dll'", this is because php_mysql.dll or libmysql.dll cannot be found by the system.
Runtime Configuration
The behavior of the MySQL
functions is affected by settings in the php.ini file.MySQL configuration options:
Name
|
Default
|
Description
|
Changeable
|
mysql.allow_persistent
|
"1"
|
Whether or not to allow persistent connections
|
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 (persistent
connections included)
|
PHP_INI_SYSTEM
|
mysql.trace_mode
|
"0"
|
Trace mode. When set to "1", warnings and SQL-errors
will be displayed. Available since PHP 4.3
|
PHP_INI_ALL
|
mysql.default_port
|
NULL
|
The default TCP port number to use
|
PHP_INI_ALL
|
mysql.default_socket
|
NULL
|
The default socket name to use. Available since PHP 4.0.1
|
PHP_INI_ALL
|
mysql.default_host
|
NULL
|
The default server host to use (doesn't apply in SQL safe mode)
|
PHP_INI_ALL
|
mysql.default_user
|
NULL
|
The default user name to use (doesn't apply in SQL safe mode)
|
PHP_INI_ALL
|
mysql.default_password
|
NULL
|
The default password to use (doesn't apply in SQL safe mode)
|
PHP_INI_ALL
|
mysql.connect_timeout
|
"60"
|
Connection timeout in seconds
|
PHP_INI_ALL
|
Resource Types
There are two resource types used
in the MySQL extension. The first one is the link_identifier for a database
connection, the second is a resource which holds the result of a query.Note: Most MySQL functions accept link_identifier as the last optional parameter. If it is not provided, the last opened connection is used.
PHP MySQL Functions
PHP: indicates the earliest version of PHP
that supports the function.
Function
|
Description
|
PHP
|
Returns the number of affected rows in the previous MySQL
operation
|
3
|
|
mysql_change_user()
|
Deprecated. Changes the user of the
current MySQL connection
|
3
|
Returns the name of the character set for the current connection
|
4
|
|
Closes a non-persistent MySQL connection
|
3
|
|
Opens a non-persistent MySQL connection
|
3
|
|
mysql_create_db()
|
Deprecated. Creates a new MySQL
database. Use mysql_query() instead
|
3
|
Moves the record pointer
|
3
|
|
Returns a database name from a call to mysql_list_dbs()
|
3
|
|
mysql_db_query()
|
Deprecated. Sends a MySQL query. Use
mysql_select_db() and mysql_query() instead
|
3
|
mysql_drop_db()
|
Deprecated. Deletes a MySQL database.
Use mysql_query() instead
|
3
|
Returns the error number of the last MySQL operation
|
3
|
|
Returns the error description of the last MySQL operation
|
3
|
|
mysql_escape_string()
|
Deprecated. Escapes a string for use in
a mysql_query. Use mysql_real_escape_string() instead
|
4
|
Returns a row from a recordset as an associative array and/or a
numeric array
|
3
|
|
Returns a row from a recordset as an associative array
|
4
|
|
Returns column info from a recordset as an object
|
3
|
|
Returns the length of the contents of each field in a result row
|
3
|
|
Returns a row from a recordset as an object
|
3
|
|
Returns a row from a recordset as a numeric array
|
3
|
|
Returns the flags associated with a field in a recordset
|
3
|
|
Returns the maximum length of a field in a recordset
|
3
|
|
Returns the name of a field in a recordset
|
3
|
|
Moves the result pointer to a specified field
|
3
|
|
Returns the name of the table the specified field is in
|
3
|
|
Returns the type of a field in a recordset
|
3
|
|
Free result memory
|
3
|
|
Returns MySQL client info
|
4
|
|
Returns MySQL host info
|
4
|
|
Returns MySQL protocol info
|
4
|
|
Returns MySQL server info
|
4
|
|
Returns information about the last query
|
4
|
|
Returns the AUTO_INCREMENT ID generated from the previous INSERT
operation
|
3
|
|
Lists available databases on a MySQL server
|
3
|
|
mysql_list_fields()
|
Deprecated. Lists MySQL table fields.
Use mysql_query() instead
|
3
|
Lists MySQL processes
|
4
|
|
mysql_list_tables()
|
Deprecated. Lists tables in a MySQL
database. Use mysql_query() instead
|
3
|
Returns the number of fields in a recordset
|
3
|
|
Returns the number of rows in a recordset
|
3
|
|
Opens a persistent MySQL connection
|
3
|
|
Pings a server connection or reconnects if there is no
connection
|
4
|
|
Executes a query on a MySQL database
|
3
|
|
Escapes a string for use in SQL statements
|
4
|
|
Returns the value of a field in a recordset
|
3
|
|
Sets the active MySQL database
|
3
|
|
Returns the current system status of the MySQL server
|
4
|
|
mysql_tablename()
|
Deprecated. Returns the table name of
field. Use mysql_query() instead
|
3
|
Returns the current thread ID
|
4
|
|
Executes a query on a MySQL database (without fetching /
buffering the result)
|
4
|
PHP MySQL Constants
Since PHP 4.3 it has been possible
to specify additional flags for the mysql_connect() and mysql_pconnect()
functions:PHP: indicates the earliest version of PHP that supports the constant.
Constant
|
Description
|
PHP
|
MYSQL_CLIENT_COMPRESS
|
Use compression protocol
|
4.3
|
MYSQL_CLIENT_IGNORE_SPACE
|
Allow space after function names
|
4.3
|
MYSQL_CLIENT_INTERACTIVE
|
Allow interactive timeout seconds of inactivity before closing
the connection
|
4.3
|
MYSQL_CLIENT_SSL
|
Use SSL encryption (only available with version 4+ of the MySQL
client library)
|
4.3
|
Constant
|
Description
|
PHP
|
MYSQL_ASSOC
|
Columns are returned into the array with the fieldname as the
array index
|
|
MYSQL_BOTH
|
Columns are returned into the array having both a numerical
index and the fieldname as the array index
|
|
MYSQL_NUM
|
Columns are returned into the array having a numerical index
(index starts at 0)
|
|