MySQL Functions MySQL These functions allow you to access MySQL database servers. More information about MySQL can be found at &url.mysql;. Documentation for MySQL can be found at &url.mysql.docs;.
Requirements In order to have these functions available, you must compile PHP with MySQL support
Installation By using the configuration option you enable PHP to access MySQL databases. If you use this option without specifying the path to MySQL, PHP will use the built-in MySQL client libraries. With PHP4 MySQL support is always eanbled; if you don't specify the configure option, the bundled libraries are used. Users who run other applications that use MySQL (for example, running PHP 3 and PHP 4 as concurrent apache modules, or auth-mysql) should always specify the path to MySQL: . This will force PHP to use the client libraries installed by MySQL, avoiding any conflicts.
Runtime Configuration The behaviour of the MySQL functions is affected by settings in the global configuration file. <link linkend="ini.sect.mysql">MySQL Configuration</link> Options Name Default Changeable mysql.allow_persistent "On" PHP_INI_SYSTEM mysql.max_persistent "-1" PHP_INI_SYSTEM mysql.max_links "-1" PHP_INI_SYSTEM mysql.default_port NULL PHP_INI_ALL mysql.default_socket NULL PHP_INI_ALL mysql.default_host NULL PHP_INI_ALL mysql.default_user NULL PHP_INI_ALL
For further details and definition of the PHP_INI_* constants see ini_set.
Resource types There are two resource types used in the MySQL module. The first one is the link identifier for a database connection, the second a resource which helds the result of a query.
Predefined constants The function mysql_fetch_array uses a constant for the different types of result arrays. The following constants are defined: MySQL fetch constants constant meaning MYSQL_ASSOC Columns are returned into the array having 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 to the fields. This index starts with 0, the first field in the result.
Examples This simple example shows how to connect, execute a query, print resulting rows and disconnect from a MySQL database. MySQL extension overview example \n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t\n"; foreach ($line as $col_value) { print "\t\t$col_value\n"; } print "\t\n"; } print "\n"; /* Free resultset */ mysql_free_result($result); /* Closing connection */ mysql_close($link); ?> ]]>
mysql_affected_rows Get number of affected rows in previous MySQL operation Description intmysql_affected_rows resource link_identifier mysql_affected_rows returns the number of rows affected by the last INSERT, UPDATE or DELETE query associated with link_identifier. If the link identifier isn't specified, the last link opened by mysql_connect is assumed. If you are using transactions, you need to call mysql_affected_rows after your INSERT, UPDATE, or DELETE query, not after the commit. If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero. When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possiblity that mysql_affected_rows may not actually equal the number of rows matched, only the number of rows that were literally affected by the query. mysql_affected_rows does not work with SELECT statements; only on statements which modify records. To retrieve the number of rows returned by a SELECT, use mysql_num_rows. If the last query failed, this function will return -1. See also: mysql_num_rows. mysql_change_user Change logged in user of the active connection Description intmysql_change_user stringuser stringpassword string database resource link_identifier mysql_change_user changes the logged in user of the current active connection, or the connection given by the optional link_identifier parameter. If a database is specified, this will be the current database after the user has been changed. If the new user and password authorization fails, the current connected user stays active. &return.success; This function was introduced in PHP 3.0.13 and requires MySQL 3.23.3 or higher. It is not available in PHP 4. mysql_character_set_name Returns the name of the character set Description intmysql_character_set_name resourcelink_identifier mysql_character_set_name returns the default character set name for the current connection. <function>mysql_character_set_name</function> example ]]> The above example would produce the following output: See also: mysql_real_escape_string mysql_close Close MySQL connection Description boolmysql_close resource link_identifier &return.success; mysql_close closes the connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used. Using mysql_close isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources. mysql_close will not close persistent links created by mysql_pconnect. MySQL close example ]]> See also: mysql_connect, and mysql_pconnect. mysql_connect Open a connection to a MySQL Server Description resourcemysql_connect string server string username string password bool new_link Returns a MySQL link identifier on success, or &false; on failure. mysql_connect establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password. The server parameter can also include a port number. eg. "hostname:port" or a path to a socket eg. ":/path/to/socket" for the localhost. Support for ":port" was added in PHP 3.0B4. Support for ":/path/to/socket" was added in PHP 3.0.10. You can suppress the error message on failure by prepending a @ to the function name. If a second call is made to mysql_connect with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect always open a new link, even if mysql_connect was called before with the same parameters. The new_link parameter became available in PHP 4.2.0 The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close. MySQL connect example ]]> See also mysql_pconnect and mysql_close. mysql_create_db Create a MySQL database Description boolmysql_create_db stringdatabase name resource link_identifier mysql_create_db attempts to create a new database on the server associated with the specified link identifier. &return.success; MySQL create database example ]]> For downwards compatibility mysql_createdb can also be used. This is deprecated, however. See also: mysql_drop_db. mysql_data_seek Move internal result pointer Description boolmysql_data_seek resourceresult_identifier introw_number &return.success; mysql_data_seek moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to mysql_fetch_row would return that row. Row_number starts at 0. MySQL data seek example = 0; $i--) { if (!mysql_data_seek($result, $i)) { echo "Cannot seek to row $i\n"; continue; } if(!($row = mysql_fetch_object($result))) continue; echo "$row->last_name $row->first_name
\n"; } mysql_free_result($result); ?> ]]>
mysql_db_name Get result data Description stringmysql_db_name resourceresult introw mixed field mysql_db_name takes as its first parameter the result pointer from a call to mysql_list_dbs. The row parameter is an index into the result set. If an error occurs, &false; is returned. Use mysql_errno and mysql_error to determine the nature of the error. <function>mysql_db_name</function> example ]]> For backward compatibility, mysql_dbname is also accepted. This is deprecated, however. mysql_db_query Send a MySQL query Description resourcemysql_db_query stringdatabase stringquery resource link_identifier Returns a positive MySQL result resource to the query result, or &false; on error. mysql_db_query selects a database and executes a query on it. If the optional link identifier isn't specified, the function will try to find an open link to the MySQL server and if no such link is found it'll try to create one as if mysql_connect was called with no arguments See also mysql_connect and mysql_query. This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db and mysql_query instead. mysql_drop_db Drop (delete) a MySQL database Description boolmysql_drop_db stringdatabase_name resource link_identifier &return.success; mysql_drop_db attempts to drop (remove) an entire database from the server associated with the specified link identifier. For downward compatibility mysql_dropdb can also be used. This is deprecated, however. See also: mysql_create_db. mysql_errno Returns the numerical value of the error message from previous MySQL operation Description intmysql_errno resource link_identifier Returns the error number from the last MySQL function, or 0 (zero) if no error occurred. Errors coming back from the MySQL database backend no longer issue warnings. Instead, use mysql_errno to retrieve the error code. Note that this function only returns the error code from the most recently executed MySQL function (not including mysql_error and mysql_errno), so if you want to use it, make sure you check the value before calling another mySQL function. "; mysql_select_db("nonexistentdb"); echo mysql_errno().": ".mysql_error()."
"; $conn = mysql_query("SELECT * FROM nonexistenttable"); echo mysql_errno().": ".mysql_error()."
"; ?> ]]>
See also: mysql_error
mysql_error Returns the text of the error message from previous MySQL operation Description stringmysql_error resource link_identifier Returns the error text from the last MySQL function, or '' (the empty string) if no error occurred. Errors coming back from the MySQL database backend no longer issue warnings. Instead, use mysql_error to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error and mysql_errno), so if you want to use it, make sure you check the value before calling another MySQL function. "; mysql_select_db("nonexistentdb"); echo mysql_errno().": ".mysql_error()."
"; $conn = mysql_query("SELECT * FROM nonexistenttable"); echo mysql_errno().": ".mysql_error()."
"; ?> ]]>
See also: mysql_errno
mysql_escape_string Escapes a string for use in a mysql_query. Description stringmysql_escape_string stringunescaped_string This function will escape the unescaped_string, so that it is safe to place it in a mysql_query. mysql_escape_string does not escape % and _. This function is identical to mysql_real_escape_string except that mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. mysql_escape_string does not take a connection argument and does not respect the current charset setting. <function>mysql_real_escape_string</function> example ]]> The above example would produce the following output: See also: mysql_real_escape_string mysql_fetch_array Fetch a result row as an associative array, a numeric array, or both. Description arraymysql_fetch_array resourceresult int result_type Returns an array that corresponds to the fetched row, or &false; if there are no more rows. mysql_fetch_array is an extended version of mysql_fetch_row. In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys. If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name (by using 'field' in this example). An important thing to note is that using mysql_fetch_array is not significantly slower than using mysql_fetch_row, while it provides a significant added value. The optional second argument result_type in mysql_fetch_array is a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. This feature was added in PHP 3.0.7. MYSQL_BOTH is the default for this argument. By using MYSQL_BOTH, you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row works). For further details, see also mysql_fetch_row and mysql_fetch_assoc. <function>mysql_fetch_array</function> example \n"; echo "user_id: ".$row[0]."
\n"; echo "fullname: ".$row["fullname"]."
\n"; echo "fullname: ".$row[1]."
\n"; } mysql_free_result($result); ?> ]]>
mysql_fetch_assoc Fetch a result row as an associative array Description arraymysql_fetch_assoc resourceresult Returns an associative array that corresponds to the fetched row, or &false; if there are no more rows. mysql_fetch_assoc is equivalent to calling mysql_fetch_array with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. This is the way mysql_fetch_array originally worked. If you need the numeric indices as well as the associative, use mysql_fetch_array. If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysql_fetch_row or add alias names. See the example at the mysql_fetch_array description about aliases. An important thing to note is that using mysql_fetch_assoc is not significantly slower than using mysql_fetch_row, while it provides a significant added value. For further details, see also mysql_fetch_row and mysql_fetch_array. <function>mysql_fetch_assoc</function> ]]> mysql_fetch_field Get column information from a result and return as an object Description objectmysql_fetch_field resourceresult int field_offset Returns an object containing field information. mysql_fetch_field can be used in order to obtain information about fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by mysql_fetch_field is retrieved. The properties of the object are: name - column name table - name of the table the column belongs to max_length - maximum length of the column not_null - 1 if the column cannot be &null; primary_key - 1 if the column is a primary key unique_key - 1 if the column is a unique key multiple_key - 1 if the column is a non-unique key numeric - 1 if the column is numeric blob - 1 if the column is a BLOB type - the type of the column unsigned - 1 if the column is unsigned zerofill - 1 if the column is zero-filled <function>mysql_fetch_field</function> \n"; $meta = mysql_fetch_field($result); if (!$meta) { echo "No information available
\n"; } echo "
blob:         $meta->blob
max_length:   $meta->max_length
multiple_key: $meta->multiple_key
name:         $meta->name
not_null:     $meta->not_null
numeric:      $meta->numeric
primary_key:  $meta->primary_key
table:        $meta->table
type:         $meta->type
unique_key:   $meta->unique_key
unsigned:     $meta->unsigned
zerofill:     $meta->zerofill
"; $i++; } mysql_free_result($result); ?> ]]>
See also mysql_field_seek.
mysql_fetch_lengths Get the length of each output in a result Description arraymysql_fetch_lengths resourceresult Returns an array that corresponds to the lengths of each field in the last row fetched by mysql_fetch_row, or &false; on error. mysql_fetch_lengths stores the lengths of each result column in the last row returned by mysql_fetch_row, mysql_fetch_array, and mysql_fetch_object in an array, starting at offset 0. See also: mysql_fetch_row. mysql_fetch_object Fetch a result row as an object Description objectmysql_fetch_object resourceresult Returns an object with properties that correspond to the fetched row, or &false; if there are no more rows. mysql_fetch_object is similar to mysql_fetch_array, with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names). field; /* this is invalid */ echo $row->0; ?> ]]> Speed-wise, the function is identical to mysql_fetch_array, and almost as quick as mysql_fetch_row (the difference is insignificant). <function>mysql_fetch_object</function> example user_id; echo $row->fullname; } mysql_free_result($result); ?> ]]> See also: mysql_fetch_array and mysql_fetch_row. mysql_fetch_row Get a result row as an enumerated array Description arraymysql_fetch_row resourceresult Returns an array that corresponds to the fetched row, or &false; if there are no more rows. mysql_fetch_row fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0. Subsequent call to mysql_fetch_row would return the next row in the result set, or &false; if there are no more rows. See also: mysql_fetch_array, mysql_fetch_object, mysql_data_seek, mysql_fetch_lengths, and mysql_result. mysql_field_flags Get the flags associated with the specified field in a result Description stringmysql_field_flags resourceresult intfield_offset mysql_field_flags returns the field flags of the specified field. The flags are reported as a single word per flag separated by a single space, so that you can split the returned value using explode. The following flags are reported, if your version of MySQL is current enough to support them: "not_null", "primary_key", "unique_key", "multiple_key", "blob", "unsigned", "zerofill", "binary", "enum", "auto_increment", "timestamp". For downward compatibility mysql_fieldflags can also be used. This is deprecated, however. mysql_field_name Get the name of the specified field in a result Description stringmysql_field_name resourceresult intfield_index mysql_field_name returns the name of the specified field index. result must be a valid result identifier and field_index is the numerical offset of the field. field_index starts at 0. e.g. The index of the third field would actually be 2, the index of the fourth field would be 3 and so on. <function>mysql_field_name</function> example The above example would produce the following output: For downwards compatibility mysql_fieldname can also be used. This is deprecated, however. mysql_field_len Returns the length of the specified field Description intmysql_field_len resourceresult intfield_offset mysql_field_len returns the length of the specified field. For downward compatibility mysql_fieldlen can also be used. This is deprecated, however. mysql_field_seek Set result pointer to a specified field offset Description intmysql_field_seek resourceresult intfield_offset Seeks to the specified field offset. If the next call to mysql_fetch_field doesn't include a field offset, the field offset specified in mysql_field_seek will be returned. See also: mysql_fetch_field. mysql_field_table Get name of the table the specified field is in Description stringmysql_field_table resourceresult intfield_offset Returns the name of the table that the specifed field is in. For downward compatibility mysql_fieldtable can also be used. This is deprecated, however. mysql_field_type Get the type of the specified field in a result Description stringmysql_field_type iresourceresult intfield_offset mysql_field_type is similar to the mysql_field_name function. The arguments are identical, but the field type is returned instead. The field type will be one of "int", "real", "string", "blob", and others as detailed in the MySQL documentation. MySQL field types "; echo "The table has the following fields
"; while ($i < $fields) { $type = mysql_field_type($result, $i); $name = mysql_field_name($result, $i); $len = mysql_field_len($result, $i); $flags = mysql_field_flags($result, $i); echo $type." ".$name." ".$len." ".$flags."
"; $i++; } mysql_close(); ?> ]]>
For downward compatibility mysql_fieldtype can also be used. This is deprecated, however.
mysql_free_result Free result memory Description boolmysql_free_result resourceresult mysql_free_result will free all memory associated with the result identifier result. mysql_free_result only needs to be called if you are concerned about how much memory is being used for queries that return large result sets. All associated result memory is automatically freed at the end of the script's execution. &return.success; For downward compatibility mysql_freeresult can also be used. This is deprecated, however. mysql_insert_id Get the id generated from the previous INSERT operation Description intmysql_insert_id resource link_identifier mysql_insert_id returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed. mysql_insert_id returns 0 if the previous query does not generate an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id immediately after the query that generates the value. The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries. mysql_insert_id converts the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT, the value returned by mysql_insert_id will be incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query. mysql_list_dbs List databases available on a MySQL server Description resourcemysql_list_dbs resource link_identifier mysql_list_dbs will return a result pointer containing the databases available from the current mysql daemon. Use the mysql_tablename function to traverse this result pointer, or any function for result tables. <function>mysql_list_dbs</function> example Database . "\n"; } ?> ]]> The above example would produce the following output: The above code would just as easily work with mysql_fetch_row or other similar functions. For downward compatibility mysql_listdbs can also be used. This is deprecated however. See also mysql_db_name. mysql_list_fields List MySQL result fields Description resourcemysql_list_fields stringdatabase_name stringtable_name resource link_identifier mysql_list_fields retrieves information about the given tablename. Arguments are the database name and the table name. A result pointer is returned which can be used with mysql_field_flags, mysql_field_len, mysql_field_name, and mysql_field_type. <function>mysql_list_fields</function> example The above example would produce the following output: For downward compatibility mysql_listfields can also be used. This is deprecated however. mysql_list_processes List MySQL processes Description resourcemysql_list_processes resource link_identifier mysql_list_processes returns a result pointer describing the current server threads. <function>mysql_list_processes</function> example ]]> The above example would produce the following output: See also: mysql_thread_id mysql_list_tables List tables in a MySQL database Description resourcemysql_list_tables stringdatabase resource link_identifier mysql_list_tables takes a database name and returns a result pointer much like the mysql_db_query function. You can use the mysql_tablename function to extract the actual table names from the result pointer, or any other result table function. For downward compatibility mysql_listtables can also be used. This is deprecated however. mysql_num_fields Get number of fields in result Description intmysql_num_fields resourceresult mysql_num_fields returns the number of fields in a result set. See also: mysql_db_query, mysql_query, mysql_fetch_field, mysql_num_rows. For downward compatibility mysql_numfields can also be used. This is deprecated however. mysql_num_rows Get number of rows in result Description intmysql_num_rows resourceresult mysql_num_rows returns the number of rows in a result set. This command is only valid for SELECT statements. To retrieve the number of rows affected by a INSERT, UPDATE or DELETE query, use mysql_affected_rows. <function>mysql_num_rows</function> example ]]> See also: mysql_affected_rows, mysql_connect, mysql_select_db, and mysql_query. For downward compatibility mysql_numrows can also be used. This is deprecated however. mysql_pconnect Open a persistent connection to a MySQL server Description resourcemysql_pconnect string server stringusername stringpassword Returns a positive MySQL persistent link identifier on success, or &false; on error. mysql_pconnect establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password. The server parameter can also include a port number. eg. "hostname:port" or a path to a socket eg. ":/path/to/socket" for the localhost. Support for ":port" was added in 3.0B4. Support for the ":/path/to/socket" was added in 3.0.10. mysql_pconnect acts very much like mysql_connect with two major differences. First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection. Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close will not close links established by mysql_pconnect). This type of link is therefore called 'persistent'. Note, that these kind of links only work if you are using a module version of PHP. See the Persistent Database Connections section for more information. Using persistent connections can require a bit of tuning of your Apache and MySQL configurations to ensure that you do not exceed the number of connections allowed by MySQL. mysql_ping Ping a server connection or reconnect if there is no connection Description boolmysql_ping resource link_identifier mysql_ping checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted. This function can be used by scripts that remain idle for a long while, to check whether or not the server has closed the connection and reconnect if necessary. mysql_ping returns &true; if the connection to the server is working, otherwise &false;. See also: mysql_thread_id mysql_list_processes mysql_query Send a MySQL query Description resourcemysql_query stringquery resourcelink_identifier mysql_query sends a query to the currently active database on the server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is assumed. If no link is open, the function tries to establish a link as if mysql_connect was called with no arguments, and use it. The query string should not end with a semicolon. Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query returns a resource identifier or &false; if the query was not executed correctly. For other type of SQL statements, mysql_query returns &true; on success and &false; on error. A non-&false; return value means that the query was legal and could be executed by the server. It does not indicate anything about the number of rows affected or returned. It is perfectly possible for a query to succeed but affect no rows or return no rows. The following query is syntactically invalid, so mysql_query fails and returns &false;: <function>mysql_query</function> ]]> The following query is semantically invalid if my_col is not a column in the table my_tbl, so mysql_query fails and returns &false;: <function>mysql_query</function> ]]> mysql_query will also fail and return &false; if you don't have permission to access the table(s) referenced by the query. Assuming the query succeeds, you can call mysql_num_rows to find out how many rows were returned for a SELECT statment or mysql_affected_rows to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement. Only for SELECT,SHOW,DESCRIBE or EXPLAIN statements, mysql_query returns a new result identifier that you can pass to mysql_fetch_array and other functions dealing with result tables. When you are done with the result set, you can free the resources associated with it by calling mysql_free_result. Although, the memory will automatically be freed at the end of the script's execution. See also: mysql_num_rows, mysql_affected_rows, mysql_unbuffered_query, mysql_free_result, mysql_fetch_array, mysql_fetch_row, mysql_fetch_assoc, mysql_result, mysql_select_db, and mysql_connect. mysql_unbuffered_query Send an SQL query to MySQL, without fetching and buffering the result rows Description resourcemysql_unbuffered_query stringquery resourcelink_identifier mysql_unbuffered_query sends a SQL query query to MySQL, without fetching and buffering the result rows automatically, as mysql_query does. On the one hand, this saves a considerable amount of memory with SQL queries that produce large result sets. On the other hand, you can start working on the result set immediately after the first row has been retrieved: you don't have to wait until the complete SQL query has been performed. When using multiple DB-connects, you have to specify the optional parameter link_identifier. The benefits of mysql_unbuffered_query come at a cost: You cannot use mysql_num_rows on a result set returned from mysql_unbuffered_query. You also have to fetch all result rows from an unbuffered SQL query, before you can send a new SQL query to MySQL. See also: mysql_query. mysql_real_escape_string Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection. Description stringmysql_real_escape_string stringunescaped_string resourcelink_identifier This function will escape special characters in the unescaped_string, taking into account the current charset of the connection so that it is safe to place it in a mysql_query. mysql_real_escape_string does not escape % and _. <function>mysql_real_escape_string</function> example ]]> The above example would produce the following output: See also: mysql_escape_string mysql_character_set_name mysql_result Get result data Description mixedmysql_result resourceresult introw mixed field mysql_result returns the contents of one cell from a MySQL result set. The field argument can be the field's offset, or the field's name, or the field's table dot field name (tablename.fieldname). If the column name has been aliased ('select foo as bar from...'), use the alias instead of the column name. When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result. Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument. Calls to mysql_result should not be mixed with calls to other functions that deal with the result set. Recommended high-performance alternatives: mysql_fetch_row, mysql_fetch_array, and mysql_fetch_object. mysql_select_db Select a MySQL database Description boolmysql_select_db stringdatabase_name resource link_identifier &return.success; mysql_select_db sets the current active database on the server that's associated with the specified link identifier. If no link identifier is specified, the last opened link is assumed. If no link is open, the function will try to establish a link as if mysql_connect was called without arguments, and use it. Every subsequent call to mysql_query will be made on the active database. See also: mysql_connect, mysql_pconnect, and mysql_query. For downward compatibility mysql_selectdb can also be used. This is deprecated however. mysql_tablename Get table name of field Description stringmysql_tablename resourceresult inti mysql_tablename takes a result pointer returned by the mysql_list_tables function as well as an integer index and returns the name of a table. The mysql_num_rows function may be used to determine the number of tables in the result pointer. <function>mysql_tablename</function> Example "; } ?> ]]> mysql_thread_id Return the current thread id Description intmysql_thread_id resourcelink_identifier mysql_thread_id returns the current thread id. If the connection is lost and you reconnect with mysql_ping, the thread ID will change. This means you should not get the thread ID and store it for later. You should get it when you need it. <function>mysql_list_processes</function> example ]]> The above example would produce the following output: See also: mysql_ping mysql_list_processes mysql_get_client_info Get MySQL client info Description stringmysql_get_client_info mysql_get_client_info returns a string that represents the client library version. mysql_get_client_info was added in PHP 4.0.5. mysql_get_host_info Get MySQL host info Description stringmysql_get_host_info resourcelink_identifier mysql_get_host_info returns a string describing the type of connection in use for the connection link_identifier, including the server host name. If link_identifier is omitted, the last opened connection will be used. mysql_get_host_info was added in PHP 4.0.5. mysql_get_proto_info Get MySQL protocol info Description intmysql_get_proto_info resourcelink_identifier mysql_get_proto_info returns the protocol version used by connection link_identifier. If link_identifier is omitted, the last opened connection will be used. mysql_get_proto_info was added in PHP 4.0.5. mysql_get_server_info Get MySQL server info Description stringmysql_get_server_info resourcelink_identifier mysql_get_server_info returns the server version used by connection link_identifier. If link_identifier is omitted, the last opened connection will be used. mysql_get_server_info was added in PHP 4.0.5. mysql_stat Get current system status Description stringmysql_stat resourcelink_identifier mysql_stat returns the current server status. mysql_stat currently only returns status for uptime, threads, queries, open tables, flush tables and queries per second. For a complete list of other status variables you have to use the SHOW STATUS sql command. <function>mysql_stat</function> example ]]> The above example would produce the following output: