Current Location: Home> Function Categories> mysql_fetch_assoc

mysql_fetch_assoc

Get a row from the result set as an associative array.
Name:mysql_fetch_assoc
Category:Uncategorized
Programming Language:php
One-line Description:Get a row from the result set as an associative array

Function name: mysql_fetch_assoc()

Applicable version: PHP 4, PHP 5

Usage: The mysql_fetch_assoc() function takes a row from the result set as an associative array. Returns the next row in the result set as an associative array, or returns false when there are no more rows.

Syntax: mysql_fetch_assoc ( resource $result): array|false

parameter:

  • result: Required. The result set identifier returned after the query is executed by the mysql_query() function.

Return value:

  • Returns an associative array where the key is the field name in the result set and the value is the value of the corresponding field.
  • Returns false when there are no more rows.

Example:

 // 连接数据库$link = mysql_connect('localhost', 'user', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } // 选择数据库mysql_select_db('database_name'); // 执行查询$result = mysql_query('SELECT * FROM table_name'); // 获取结果集中的每一行作为关联数组while ($row = mysql_fetch_assoc($result)) { // 输出关联数组的值echo $row['column_name1'] . ', ' . $row['column_name2'] . '<br>'; } // 释放结果集mysql_free_result($result); // 关闭数据库连接mysql_close($link);

Notes:

  • The mysql_fetch_assoc() function has been abandoned in PHP 5.5.0 and is no longer recommended. It is recommended to use the mysqli or PDO_MySQL extension instead.
  • In PHP 7.0.0, the mysql_fetch_assoc() function has been removed and cannot be used.
  • When using this function, you need to first connect to the database through the mysql_connect() function, and select the database to operate through the mysql_select_db() function.
  • Use the mysql_fetch_assoc() function in a loop to get the data in the result set line by line and access the value of each field using the associative array.
  • After using the result set, the result set should be released using the mysql_free_result() function.
  • Finally, use the mysql_close() function to close the database connection and free up the resources.
Similar Functions
Popular Articles