banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

Collection of Mysql fetch operations in PHP learning notes

Explanation: Recently, I have organized some related notes on my PHP learning and would like to share them with you here. In addition, I have been studying the source code of WP recently and have gained some insights. I plan to share them with you after the New Year holiday. I have been quite busy lately, so the updates on my blog are not as frequent as before. However, I will maintain a pace of updating content once or twice a day. If you like my blog posts, you are welcome to subscribe. I usually update my blog after 9 PM in the evening, so you can ensure that you will see my posts first thing in the morning.

Key functions: mysql_fetch_array(); mysql_fetch_assoc(); mysql_fetch_row(); mysql_fetch_object(); mysql_fetch_fields(); mysql_fetch_lengths(); 1. mysql_fetch_array() This is one of the most commonly used fetch operations. It retrieves a row from the result set as an associative array, numeric array, or both. The array is indexed by column names or numbers in the result set, and the returned array can be controlled by parameters.

Function prototype: array mysql_fetch_array (resource $result [, int $ result_type ]) Parameters: resource $result The result of the MySQL query ?int $ result_type The result type parameter, which can be MYSQL_ASSOC, MYSQL_NUM, or MYSQL_BOTH. Returns: The result array. If there is no data, it returns FALSE.

MYSQL_ASSOC returns an array indexed by associative names, usually column names. MYSQL_NUM returns an array indexed by numbers. MYSQL_BOTH returns all the data, which is the default.

In addition, mysql_fetch_assoc() was added in PHP 4.0.0.3 and later versions. Its functionality is the same as mysql_fetch_array($result, MYSQL_ASSOC). Therefore, it is recommended to use mysql_fetch_assoc() in higher versions of PHP.

  1. mysql_fetch_assoc() returns an array indexed by associative names and does not return a numeric indexed array. It can only be used in PHP versions 4.0.0.3 and above.

Function prototype: array mysql_fetch_assoc(resource $result) Parameters: resource $result The result of the MySQL query Returns: The result array. If there is no data, it returns FALSE.

  1. mysql_fetch_row() returns an array generated based on the retrieved row. It returns one row, and if called in a loop, it returns subsequent rows. The initial cursor offset is 0. This function is the earliest MySQL result fetch operation and is currently mainly used in older versions of PHP or replaced by mysql_fetch_array(). The manual says that fetch_array() is faster, which may depend on the PHP version.

Function prototype: array mysql_fetch_row(resource $result) Parameters: resource $result The result of the MySQL query Returns: The result array. If there is no data, it returns FALSE.

  1. mysql_fetch_fields() retrieves column information from the result set and returns it as an object. It returns the column-related information in the result set and can return the column information in the result set according to requirements. In PHP 5, it mainly includes using column offsets to distinguish between columns, starting with an offset of 0.

name - The column name
table - The table in which the column is located
max_length - The 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 prototype: Array mysql_fetch_fields(resource $result [, int $field_offset]) Parameters: resource $result The result of the MySQL query ?int $field_offset The offset, with the default cursor incrementing by 1 for the next column Returns: The result array. If there is no data, it returns FALSE.

  1. mysql_fetch_lengths() stores the length of each column in the result of the previous mysql_fetch_row(), mysql_fetch_array(), and mysql_fetch_object() operations in an array. The initial offset is 0. This function is mainly used for checking the returned results and obtaining the length of the returned array data.

Function prototype: array mysql_fetch_lengths(resource $result) Parameters: resource $result The result of the MySQL query Returns: The result array. If there is no data, it returns FALSE.

  1. mysql_fetch_object() fetches the result as an object. The returned data can only be accessed through object properties. PHP also retains numeric indexing for access.

Function prototype: array mysql_fetch_object(resource $result) Parameters: resource $result The result of the MySQL query Returns: The result array. If there is no data, it returns FALSE.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.