Function name: mysql_field_name()
Applicable version: PHP 4, PHP 5
Usage: The mysql_field_name() function is used to get the name of the specified field.
Syntax: string mysql_field_name ( resource $result , int $field_offset )
parameter:
Return value: Returns the name of the specified field, and returns FALSE if an error occurs.
Example:
<?php // 建立MySQL 连接$link = mysql_connect("localhost", "username", "password"); if (!$link) { die('连接失败:' . mysql_error()); } // 选择数据库$db_selected = mysql_select_db("database_name", $link); if (!$db_selected) { die('无法使用数据库:' . mysql_error()); } // 执行查询$result = mysql_query("SELECT * FROM table_name", $link); if (!$result) { die('查询失败:' . mysql_error()); } // 获取第一个字段的名称$field_name = mysql_field_name($result, 0); echo "第一个字段的名称是:" . $field_name; // 关闭连接mysql_close($link); ?>
In the above example, we first establish a connection to the MySQL database and select the specified database. We then execute a query and save the query results in the $result
variable. Next, use mysql_field_name()
function to get the name of the first field in the query result and print it out. Finally, the connection to the database is closed.
Please note that mysql_field_name()
function has been deprecated in PHP 7, and it is recommended to use the mysqli or PDO extension instead.