ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP MySQL

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
ÓÃPHP5µÄDirector..
PHP ×èÖ¹SQL×¢ÈëÊ..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
ÓÃPHP¿ØÖÆÓû§µÄä..
PHP PDO ѧϰ±Ê¼Ç

PHP MySQL


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-06-17 ä¯ÀÀ:6094 :: ::

PHP MySQL Introduction
PHP MySQL º¯Êý½éÉÜ

The MySQL functions allows you to access MySQL database servers.
¿ÉÒÔʹÓñ¾ÀຯÊý·ÃÎÊ MySQL Êý¾Ý¿â·þÎñÆ÷¡£


Installation
°²×°

For the MySQL functions to be available, you must compile PHP with MySQL support.
ΪÁËÄܹ»Ë³ÀûµÄʹÓñ¾ÀຯÊý£¬±ØÐëÔÚ±àÒë PHP µÄʱºòÌí¼Ó MySQL µÄÖ§³Ö¡£

For compiling, use --with-mysql=DIR (the optional DIR points to the MySQL directory).
±àÒëʱ£¬Ö»ÒªÊ¹Óà --with-mysql[=DIR] ÅäÖÃÑ¡Ïî¼´¿É£¬ÆäÖпÉÑ¡µÄ [DIR] Ö¸Ïò MySQL µÄ°²×°Ä¿Â¼¡£

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.
×¢Ò⣺±¾ MySQL À©Õ¹¿â²»Ö§³Ö MySQL 4.1.0 ÒÔÉϰ汾ȫ²¿¹¦ÄÜ¡£¶Ô´Ë£¬¿ÉÓà MySQLi¡£Èç¹ûҪͬʱ°²×° mysql À©Õ¹¿âºÍ mysqli À©Õ¹¿â£¬±ØÐëʹÓÃͬһ¸ö¿Í»§¶Ë¿âÒÔ±ÜÃâÈκγåÍ»¡£

Installation on Linux Systems
Linuxϵͳϵݲװ

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 www.mysql.com.
PHP 5+£ºMySQL ĬÈÏδÆôÓã¬Ò²Ã»ÓÐ°ó¶¨µÄ MySQL ¿â¡£Ê¹Óà --with-mysql[=DIR] ÅäÖÃÑ¡ÏîÀ´¼ÓÈë MySQL µÄÖ§³Ö¡£¿ÉÒÔ´Ó MySQL.COM ÏÂÔØÍ·ÎļþºÍ¿â¡£

Installation on Windows Systems
Windowsϵͳϵݲװ

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.
PHP 5+: MySQL ĬÈÏδÆôÓã¬Òò´Ë±ØÐëÔÚ php.ini Ö줻î php_mysql.dll ¶¯Ì¬Á¬½Ó¿â¡£´ËÍ⣬PHP »¹ÐèÒª·ÃÎÊ MySQL ¿Í»§¶ËÁ¬½Ó¿â¡£ PHP µÄ Windows ·¢Ðаæ°üÀ¨ÁËÒ»¸ö libmysql.dll£¬ÎªÁËÈà PHP ÄÜºÍ MySQL ¶Ô»°£¬´ËÎļþ±ØÐë·ÅÔÚ Windows µÄϵͳ·¾¶ 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:phpext.
Òª¼¤»îÈκΠPHP À©Õ¹¿â£¨ÀýÈç php_mysql.dll£©£¬PHP Ö¸Áî extension_dir Òª±»ÉèΪ PHP À©Õ¹¿âËùÔÚµÄĿ¼¡£ PHP 5 Ï extension_dir ȡֵµÄÒ»¸öÀý×ÓÊÇ c:phpext¡£

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.
×¢Ò⣺Èç¹ûÆô¶¯ web ·þÎñÆ÷ʱ³öÏÖÀàËÆÈçϵĴíÎó£º"Unable to load dynamic library './php_mysql.dll'"£¬ÕâÊÇÒòΪϵͳÕÒ²»µ½ php_mysql.dll ºÍ£¯»ò libmysql.dll¡£


Runtime Configuration
ÔËÐÐʱÅäÖÃ

The behavior of the MySQL functions is affected by settings in the php.ini file.
ÕâЩº¯ÊýµÄÐÐΪÊÜ php.ini µÄÓ°Ïì¡£

MySQL configuration options:
MySQLÅäÖÃÑ¡Ïî

Name
Ãû³Æ
Default
ĬÈÏÖµ
Description
˵Ã÷
Changeable
¿ÉÐ޸ķ¶Î§
mysql.allow_persistent "1" Whether or not to allow persistent connections
ÊÇ·ñÔÊÐí MySQL µÄ³Ö¾ÃÁ¬½Ó
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
¸ú×Ùģʽ¡£µ±¼¤»î mysql.trace_mode ʱ£¬½«»áÏÔʾ table/index ɨÃèµÄ¾¯¸æ£¬Î´ÊͷŵĽá¹û¼¯ÒÔ¼° SQL ´íÎó¡££¨×ÔPHP 4.3.0£©
PHP_INI_ALL
mysql.default_port NULL The default TCP port number to use
Ö¸¶¨Ä¬ÈÏÁ¬½ÓÊý¾Ý¿âµÄ TCP ¶Ë¿ÚºÅ¡£
PHP_INI_ALL
mysql.default_socket NULL The default socket name to use. Available since PHP 4.0.1
µ±Ê¹Óñ¾µØÁ¬½ÓµÄʱºò£¬Ä¬È쵀 socket Ãû³Æ¡£
PHP_INI_ALL
mysql.default_host NULL The default server host to use (doesn't apply in SQL safe mode)
ĬÈÏÁ¬½ÓµÄÊý¾Ý¿â·þÎñÆ÷µØÖ·¡£²»ÊÊÓÃÓÚ SQL °²È«Ä£Ê½¡£
PHP_INI_ALL
mysql.default_user NULL The default user name to use (doesn't apply in SQL safe mode)
ĬÈÏÁ¬½ÓÊý¾Ý¿âʱʹÓõÄÓû§Ãû¡£²»ÊÊÓÃÓÚ SQL °²È«Ä£Ê½¡£
PHP_INI_ALL
mysql.default_password NULL The default password to use (doesn't apply in SQL safe mode)
ĬÈÏÁ¬½ÓÊý¾Ý¿âʱʹÓõÄÃÜÂë¡£²»ÊÊÓÃÓÚ SQL °²È«Ä£Ê½¡£
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.
ÔÚ MySQL Ä£¿éÖÐʹÓÃÁËÁ½ÖÖ×ÊÔ´ÀàÐÍ¡£µÚÒ»ÖÖÊÇÊý¾Ý¿âµÄÁ¬½Ó¾ä±ú£¬µÚ¶þÖÖÊÇ SQL ²éѯ·µ»ØµÄ½á¹û¼¯¡£

Note: Most MySQL functions accept link_identifier as the last optional parameter. If it is not provided, the last opened connection is used.
×¢Ò⣺´ó¶àÊý MySQL º¯Êý¶¼½ÓÊÜ link_identifier ×÷Ϊ×îºóÒ»¸ö¿ÉÑ¡²ÎÊý¡£Èç¹ûδÌṩ´Ë²ÎÊý£¬ÔòʹÓÃ×îºóÒ»¸ö´ò¿ªµÄÁ¬½Ó¡£


PHP MySQL Functions
PHP MySQLº¯Êý

PHP: indicates the earliest version of PHP that supports the function.
PHP£º×îÔçÖ§³ÖÕâЩº¯ÊýµÄPHP°æ±¾

Function
º¯Êý
Description
˵Ã÷
PHP
mysql_affected_rows() Returns the number of affected rows in the previous MySQL operation
È¡µÃǰһ´Î MySQL ²Ù×÷ËùÓ°ÏìµÄ¼Ç¼ÐÐÊý
3
mysql_change_user() Deprecated. Changes the user of the current MySQL connection
²»ÍƼö¡£¸Ä±ä»î¶¯Á¬½ÓÖеǼµÄÓû§¡£
3
mysql_client_encoding() Returns the name of the character set for the current connection
·µ»Ø×Ö·û¼¯µÄÃû³Æ
4
mysql_close() Closes a non-persistent MySQL connection
¹Ø±Õ MySQL Á¬½Ó
3
mysql_connect() Opens a non-persistent MySQL connection
´ò¿ªÒ»¸öµ½ MySQL ·þÎñÆ÷µÄÁ¬½Ó
3
mysql_create_db() Deprecated. Creates a new MySQL database. Use mysql_query() instead
²»ÍƼö¡£½¨Á¢Ò»¸öеÄMySQLÊý¾Ý¿â£¬¸Ãº¯ÊýÓÃmysql_query() º¯ÊýÀ´´úÌæ
3
mysql_data_seek() Moves the record pointer
ÒÆ¶¯ÄÚ²¿½á¹ûµÄÖ¸Õë
3
mysql_db_name() Returns a database name from a call to mysql_list_dbs()
»ñÈ¡Êý¾Ý¿âÃû³Æ£¨¸ÃÊý¾Ý¿âÊÇͨ¹ýÇëÇómysql_list_dbs()º¯ÊýÁоٵģ©
3
mysql_db_query() Deprecated. Sends a MySQL query. Use mysql_select_db() and mysql_query() instead
²»ÍƼö¡£·¢ËÍÒ»Ìõ MySQL ²éѯ¡£ÓÃmysql_select_db() ºÍmysql_query() À´´úÌæ
3
mysql_drop_db() Deprecated. Deletes a MySQL database. Use mysql_query() instead
²»ÍƼö¡£¶ªÆú£¨É¾³ý£©Ò»¸ö MySQL Êý¾Ý¿â¡£ÓÃmysql_query() ´úÌæ
3
mysql_errno() Returns the error number of the last MySQL operation
·µ»ØÉÏÒ»¸ö MySQL ²Ù×÷ÖеĴíÎóÐÅÏ¢µÄÊý×Ö±àÂë
3
mysql_error() Returns the error description of the last MySQL operation
·µ»ØÉÏÒ»¸ö MySQL ²Ù×÷²úÉúµÄÎı¾´íÎóÐÅÏ¢
3
mysql_escape_string() Deprecated. Escapes a string for use in a mysql_query. Use mysql_real_escape_string() instead
²»ÍƼö¡£×ªÒåÒ»¸ö×Ö·û´®ÓÃÓÚ mysql_query¡£ÓÃmysql_real_escape_string()´úÌæ
4
mysql_fetch_array() Returns a row from a recordset as an associative array and/or a numeric array
´Ó½á¹û¼¯ÖÐÈ¡µÃÒ»ÐÐ×÷Ϊ¹ØÁªÊý×飬»òÊý×ÖÊý×飬»ò¶þÕß¼æÓÐ
3
mysql_fetch_assoc() Returns a row from a recordset as an associative array
´Ó½á¹û¼¯ÖÐÈ¡µÃÒ»ÐÐ×÷Ϊ¹ØÁªÊý×é
4
mysql_fetch_field() Returns column info from a recordset as an object
´Ó½á¹û¼¯ÖÐÈ¡µÃÁÐÐÅÏ¢²¢×÷Ϊ¶ÔÏó·µ»Ø
3
mysql_fetch_lengths() Returns the length of the contents of each field in a result row
È¡µÃ½á¹û¼¯ÖÐÿ¸öÊä³öµÄ³¤¶È
3
mysql_fetch_object() Returns a row from a recordset as an object
´Ó½á¹û¼¯ÖÐÈ¡µÃÒ»ÐÐ×÷Ϊ¶ÔÏó
3
mysql_fetch_row() Returns a row from a recordset as a numeric array
´Ó½á¹û¼¯ÖÐÈ¡µÃÒ»ÐÐ×÷Ϊö¾ÙÊý×é
3
mysql_field_flags() Returns the flags associated with a field in a recordset
´Ó½á¹ûÖÐÈ¡µÃºÍÖ¸¶¨×ֶιØÁªµÄ±êÖ¾
3
mysql_field_len() Returns the maximum length of a field in a recordset
·µ»ØÖ¸¶¨×ֶεij¤¶È
3
mysql_field_name() Returns the name of a field in a recordset
È¡µÃ½á¹ûÖÐÖ¸¶¨×ֶεÄ×Ö¶ÎÃû
3
mysql_field_seek() Moves the result pointer to a specified field
½«½á¹û¼¯ÖеÄÖ¸ÕëÉè¶¨ÎªÖÆ¶¨µÄ×Ö¶ÎÆ«ÒÆÁ¿
3
mysql_field_table() Returns the name of the table the specified field is in
È¡µÃÖ¸¶¨×Ö¶ÎËùÔڵıíÃû
3
mysql_field_type() Returns the type of a field in a recordset
È¡µÃ½á¹û¼¯ÖÐÖ¸¶¨×ֶεÄÀàÐÍ
3
mysql_free_result() Free result memory
ÊͷŽá¹ûÄÚ´æ
3
mysql_get_client_info() Returns MySQL client info
È¡µÃ MySQL ¿Í»§¶ËÐÅÏ¢
4
mysql_get_host_info() Returns MySQL host info
È¡µÃ MySQL Ö÷»úÐÅÏ¢
4
mysql_get_proto_info() Returns MySQL protocol info
È¡µÃ MySQL ЭÒéÐÅÏ¢
4
mysql_get_server_info() Returns MySQL server info
È¡µÃ MySQL ·þÎñÆ÷ÐÅÏ¢
4
mysql_info() Returns information about the last query
È¡µÃ×î½üÒ»Ìõ²éѯµÄÐÅÏ¢
4
mysql_insert_id() Returns the AUTO_INCREMENT ID generated from the previous INSERT operation
È¡µÃÉÏÒ»²½ INSERT ²Ù×÷²úÉúµÄ ID
3
mysql_list_dbs() Lists available databases on a MySQL server
Áгö MySQL ·þÎñÆ÷ÖÐËùÓеÄÊý¾Ý¿â
3
mysql_list_fields() Deprecated. Lists MySQL table fields. Use mysql_query() instead
²»ÍƼöʹÓøú¯Êý¡£ËüÊÇÓÃÀ´ÁгöMySQL±í×ֶεġ£ÎÒÃǽ¨ÒéʹÓÃmysql_query()º¯Êý´úÌæ¡£
3
mysql_list_processes() Lists MySQL processes
Áгö MySQL ½ø³Ì
4
mysql_list_tables() Deprecated. Lists tables in a MySQL database. Use mysql_query() instead
²»ÍƼö¡£Áгö MySQL Êý¾Ý¿âÖÐµÄ±í¡£ÓÃmysql_query()´úÌæ
3
mysql_num_fields() Returns the number of fields in a recordset
È¡µÃ½á¹û¼¯ÖÐ×ֶεÄÊýÄ¿
3
mysql_num_rows() Returns the number of rows in a recordset
È¡µÃ½á¹û¼¯ÖÐÐеÄÊýÄ¿
3
mysql_pconnect() Opens a persistent MySQL connection
´ò¿ªÒ»¸öµ½ MySQL ·þÎñÆ÷µÄ³Ö¾ÃÁ¬½Ó
3
mysql_ping() Pings a server connection or reconnects if there is no connection
Ping Ò»¸ö·þÎñÆ÷Á¬½Ó£¬Èç¹ûûÓÐÁ¬½ÓÔòÖØÐÂÁ¬½Ó
4
mysql_query() Executes a query on a MySQL database
·¢ËÍÒ»Ìõ MySQL ²éѯ
3
mysql_real_escape_string() Escapes a string for use in SQL statements
תÒå SQL Óï¾äÖÐʹÓõÄ×Ö·û´®ÖеÄÌØÊâ×Ö·û£¬²¢¿¼Âǵ½Á¬½ÓµÄµ±Ç°×Ö·û¼¯
4
mysql_result() Returns the value of a field in a recordset
È¡µÃ½á¹ûÊý¾Ý
3
mysql_select_db() Sets the active MySQL database
Ñ¡Ôñ MySQL Êý¾Ý¿â
3
mysql_stat() Returns the current system status of the MySQL server
È¡µÃµ±Ç°ÏµÍ³×´Ì¬
4
mysql_tablename() Deprecated. Returns the table name of field. Use mysql_query() instead
²»ÍƼö¡£È¡µÃ±íÃû¡£ÓÃmysql_query()´úÌæ
3
mysql_thread_id() Returns the current thread ID
·µ»Øµ±Ç°Ïß³ÌµÄ ID
4
mysql_unbuffered_query() Executes a query on a MySQL database (without fetching / buffering the result)
Ïò MySQL ·¢ËÍÒ»Ìõ SQL ²éѯ£¬²¢²»»ñÈ¡ºÍ»º´æ½á¹ûµÄÐÐ
4

 


PHP MySQL Constants
PHP MySQL³£Á¿

Since PHP 4.3 it has been possible to specify additional flags for the mysql_connect() and mysql_pconnect() functions:
×ÔPHP 4.3£¬¿ÉÒÔΪmysql_connect()ºÍ mysql_pconnect()º¯ÊýÖ¸¶¨¸½¼Ó±êÖ¾

PHP: indicates the earliest version of PHP that supports the constant.
PHP£º×îÔçÖ§³ÖÕâЩ³£Á¿µÄPHP°æ±¾

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
ÔÊÐíÉèÖöϿªÁ¬½Ó֮ǰËù¿ÕÏеȺòµÄ interactive_timeout ʱ¼ä£¨´úÌæ wait_timeout£©¡£
4.3
MYSQL_CLIENT_SSL Use SSL encryption (only available with version 4+ of the MySQL client library)
ʹÓà SSL ¼ÓÃÜ¡£±¾±êÖ¾½öÔÚ MySQL ¿Í»§¶Ë¿â°æ±¾Îª 4.x »ò¸ü¸ß°æ±¾Ê±¿ÉÓá£
4.3

The mysql_fetch_array() function uses a constant for the different types of result arrays. The following constants are defined:
mysql_fetch_array() º¯ÊýʹÓÃÒ»¸ö³£Á¿À´±íʾËù·µ»ØÊý×éµÄÀàÐÍ¡£ÏÂÃæÊdz£Á¿µÄ¶¨Ò壺

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)
·µ»ØµÄÊý¾ÝÁÐʹÓÃÊý×ÖË÷Òý×÷ΪÊý×éµÄË÷ÒýÃû¡£Ë÷Òý´Ó 0 ¿ªÊ¼£¬±íʾ·µ»Ø½á¹ûµÄµÚÒ»¸ö×ֶΡ£
 

ÆÀÂÛ (0) All