Resource Type
Two resource types are used in the MySQL module. The first is the database connection handle, and the second is the result set returned by SQL query.
Predefined constants
The following constants are defined by this extension module, so they are only valid if this extension module is compiled into PHP, or is dynamically loaded at runtime.
In versions after PHP 4.3.0, more client tags are allowed to be specified in the mysql_connect() function and the mysql_pconnect() function. The defined constants are listed below:
Table 2. MySQL Client Constants
Constant Description
MYSQL_CLIENT_COMPRESS Use compressed communication protocol
MYSQL_CLIENT_IGNORE_SPACE Allows to leave spaces after function name
MYSQL_CLIENT_INTERACTIVE allows setting the interactive_timeout time (instead of wait_timeout) that is idle waiting before disconnection.
MYSQL_CLIENT_SSL is encrypted using SSL. This flag is only available when the MySQL client library version is or later. Both PHP 4 and PHP 5 installation packages for Windows are bound to 3.
The mysql_fetch_array() function uses a constant to represent the type of the returned array. The following is the definition of constants:
Table 3. MySQL fetch constant
Constant Description
The returned data column using the field name as the index name of the array.
The returned data column using the field name and numeric index as the index name of the array.
The returned data column using a numeric index as the index name of the array. The index starts at 0, indicating the first field that returns the result.
Comments
Note: Most MySQL functions accept link_identifier as the last optional parameter. If this parameter is not provided, the last open connection is used. If there is no connection, the default parameters defined in will be used to try to establish the connection. If the connection is not successful, the function returns FALSE.
example
The following simple examples demonstrate how to connect to a database, execute query statements, print return result sets, and disconnect the database. Example 1. MySQL Example
<?php
//Connect, select the database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// Execute SQL query
$query = 'Select * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Use HTML to display the results
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Release the result set
mysql_free_result($result);
// Close the connection
mysql_close($link);
?>
Table of contents
mysql_affected_rows -- Get the number of records affected by the previous MySQL operation
mysql_change_user -- Change the user logged in in the active connection
mysql_client_encoding -- Returns the name of the character set
mysql_close - Close MySQL Connection
mysql_connect --Open a connection to the MySQL server
mysql_create_db -- Create a new MySQL database
mysql_data_seek -- The pointer to move internal result
mysql_db_name -- Obtain the result data
mysql_db_query --Send a MySQL query
mysql_drop_db -- Discard (delete) a MySQL database
mysql_errno -- Returns the numerical encoding of the error message in the previous MySQL operation
mysql_error -- Returns the text error message generated by the previous MySQL operation
mysql_escape_string -- Escape a string for mysql_query
mysql_fetch_array -- Get a row from the result set as an associative array, or an array of numbers, or both
mysql_fetch_assoc -- Get a row from the result set as an associative array
mysql_fetch_field -- Get column information from the result set and return as an object
mysql_fetch_lengths -- Get the length of each output in the result set
mysql_fetch_object -- Get a row from the result set as an object
mysql_fetch_row -- Get a row from the result set as an enumeration array
mysql_field_flags -- Get the flag associated with the specified field from the result
mysql_field_len -- Return the length of the specified field
mysql_field_name -- Get the field name of the specified field in the result
mysql_field_seek -- Set the pointer in the result set to the established field offset
mysql_field_table -- Get the table name where the specified field is located
mysql_field_type -- Get the type of the specified field in the result set
mysql_free_result -- Release the result memory
mysql_get_client_info -- Get MySQL client information
mysql_get_host_info -- Get MySQL host information
mysql_get_proto_info -- Get MySQL protocol information
mysql_get_server_info -- Get MySQL server information
mysql_info -- Get the information of the latest query
mysql_insert_id -- Get the ID generated by the previous Insert operation
mysql_list_dbs -- List all databases in the MySQL server
mysql_list_fields -- List the fields in the MySQL result
mysql_list_processes -- List MySQL processes
mysql_list_tables -- List tables in MySQL database
mysql_num_fields -- The number of fields in the result set is obtained
mysql_num_rows -- The number of rows in the result set is obtained
mysql_pconnect -- Open a persistent connection to the MySQL server
mysql_ping -- Ping A server connection, if there is no connection, reconnect
mysql_query --Send a MySQL query
mysql_real_escape_string -- Escape special characters in strings used in SQL statements and take into account the current character set of the connection
mysql_result -- Get result data
mysql_select_db -- Select MySQL database
mysql_stat -- Get the current system status
mysql_tablename -- Get the table name
mysql_thread_id - Return the ID of the current thread
mysql_unbuffered_query -- Send a SQL query to MySQL, and rows that do not get and cache the results
Two resource types are used in the MySQL module. The first is the database connection handle, and the second is the result set returned by SQL query.
Predefined constants
The following constants are defined by this extension module, so they are only valid if this extension module is compiled into PHP, or is dynamically loaded at runtime.
In versions after PHP 4.3.0, more client tags are allowed to be specified in the mysql_connect() function and the mysql_pconnect() function. The defined constants are listed below:
Table 2. MySQL Client Constants
Constant Description
MYSQL_CLIENT_COMPRESS Use compressed communication protocol
MYSQL_CLIENT_IGNORE_SPACE Allows to leave spaces after function name
MYSQL_CLIENT_INTERACTIVE allows setting the interactive_timeout time (instead of wait_timeout) that is idle waiting before disconnection.
MYSQL_CLIENT_SSL is encrypted using SSL. This flag is only available when the MySQL client library version is or later. Both PHP 4 and PHP 5 installation packages for Windows are bound to 3.
The mysql_fetch_array() function uses a constant to represent the type of the returned array. The following is the definition of constants:
Table 3. MySQL fetch constant
Constant Description
The returned data column using the field name as the index name of the array.
The returned data column using the field name and numeric index as the index name of the array.
The returned data column using a numeric index as the index name of the array. The index starts at 0, indicating the first field that returns the result.
Comments
Note: Most MySQL functions accept link_identifier as the last optional parameter. If this parameter is not provided, the last open connection is used. If there is no connection, the default parameters defined in will be used to try to establish the connection. If the connection is not successful, the function returns FALSE.
example
The following simple examples demonstrate how to connect to a database, execute query statements, print return result sets, and disconnect the database. Example 1. MySQL Example
<?php
//Connect, select the database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// Execute SQL query
$query = 'Select * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Use HTML to display the results
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Release the result set
mysql_free_result($result);
// Close the connection
mysql_close($link);
?>
Table of contents
mysql_affected_rows -- Get the number of records affected by the previous MySQL operation
mysql_change_user -- Change the user logged in in the active connection
mysql_client_encoding -- Returns the name of the character set
mysql_close - Close MySQL Connection
mysql_connect --Open a connection to the MySQL server
mysql_create_db -- Create a new MySQL database
mysql_data_seek -- The pointer to move internal result
mysql_db_name -- Obtain the result data
mysql_db_query --Send a MySQL query
mysql_drop_db -- Discard (delete) a MySQL database
mysql_errno -- Returns the numerical encoding of the error message in the previous MySQL operation
mysql_error -- Returns the text error message generated by the previous MySQL operation
mysql_escape_string -- Escape a string for mysql_query
mysql_fetch_array -- Get a row from the result set as an associative array, or an array of numbers, or both
mysql_fetch_assoc -- Get a row from the result set as an associative array
mysql_fetch_field -- Get column information from the result set and return as an object
mysql_fetch_lengths -- Get the length of each output in the result set
mysql_fetch_object -- Get a row from the result set as an object
mysql_fetch_row -- Get a row from the result set as an enumeration array
mysql_field_flags -- Get the flag associated with the specified field from the result
mysql_field_len -- Return the length of the specified field
mysql_field_name -- Get the field name of the specified field in the result
mysql_field_seek -- Set the pointer in the result set to the established field offset
mysql_field_table -- Get the table name where the specified field is located
mysql_field_type -- Get the type of the specified field in the result set
mysql_free_result -- Release the result memory
mysql_get_client_info -- Get MySQL client information
mysql_get_host_info -- Get MySQL host information
mysql_get_proto_info -- Get MySQL protocol information
mysql_get_server_info -- Get MySQL server information
mysql_info -- Get the information of the latest query
mysql_insert_id -- Get the ID generated by the previous Insert operation
mysql_list_dbs -- List all databases in the MySQL server
mysql_list_fields -- List the fields in the MySQL result
mysql_list_processes -- List MySQL processes
mysql_list_tables -- List tables in MySQL database
mysql_num_fields -- The number of fields in the result set is obtained
mysql_num_rows -- The number of rows in the result set is obtained
mysql_pconnect -- Open a persistent connection to the MySQL server
mysql_ping -- Ping A server connection, if there is no connection, reconnect
mysql_query --Send a MySQL query
mysql_real_escape_string -- Escape special characters in strings used in SQL statements and take into account the current character set of the connection
mysql_result -- Get result data
mysql_select_db -- Select MySQL database
mysql_stat -- Get the current system status
mysql_tablename -- Get the table name
mysql_thread_id - Return the ID of the current thread
mysql_unbuffered_query -- Send a SQL query to MySQL, and rows that do not get and cache the results