Home > Article > Backend Development > php native MySQL API
It is not recommended to use this extension when writing new code. You should use the mysqli or PDO_MySQL extension instead.
Installation
When compiling, just use the --with-mysql[=DIR] configuration option, 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, use the MySQLi extension library.
If you want to install the mysql extension library and the mysqli extension library at the same time, you must use the same client library to avoid any conflicts.
Example
This simple example shows how to connect, execute a query, print the result set and then disconnect from the MySQL database.
Example #1 MySQL extension overview example
<?php // 连接、选择数据库 $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'); // 执行 SQL 查询 $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // 以 HTML 打印查询结果 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"; // 释放结果集 mysql_free_result($result); // 关闭连接 mysql_close($link); ?>
MySQL functions
The last optional parameter of most MySQL functions is link_identifier. If this parameter is not provided, the last open connection will be used. If the last opened connection does not exist, it will try to connect using the default parameters defined in php.ini. If the connection is not successful, the function returns FALSE.
mysql_affected_rows — Get the number of rows affected by the previous MySQL operation
mysql_client_encoding — Return the name of the character set
mysql_close — Close the MySQL connection
mysql_connect — Open a connection to the MySQL server
mysql_creat e_db — Create a new MySQL database
mysql_data_seek — Move the internal result pointer
mysql_db_name — Get the result data
mysql_db_query — Send a MySQL query
mysql_drop_db — Drop (drop) a MySQL database
mysql_errno — Returns the number of error messages in the previous MySQL operation Encoding
mysql_error — Returns the textual error message generated by the previous MySQL operation
mysql_escape_string — Escapes a string for use with mysql_query
mysql_fetch_array — Fetches a row from the result set as an associative array, or a numeric array, 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 it 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 flags 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 specified 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 the MySQL client Terminal 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 latest query information
mysql_ins ert_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 results
mysql_list_processes — List the MySQL processes
mysql_list_tables — List the tables in the MySQL database
mysql_num_fields — Get The number of fields in the result set
mysql_num_rows — Get the number of rows in the result set
mysql_pconnect — Open a persistent connection to the MySQL server
mysql_ping — Ping a server connection, reconnect if there is no connection
mysql_query — Send a MySQL query
mysql_real_ escape_string — escape Define special characters in strings used in SQL statements, taking into account the current character set of the connection
mysql_result — Get the result data
mysql_select_db — Select the MySQL database
mysql_set_charset — Set the client’s character set
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 — Sends a SQL query to MySQL without retrieving and caching the result rows