Function name: mysql_num_fields()
Applicable version: PHP 4, PHP 5, PHP 7
Usage: The mysql_num_fields() function is used to get the number of fields in the result set.
Syntax: int mysql_num_fields ( resource $result )
parameter:
Return value: Returns the number of fields in the result set, and if the result set is empty or an error occurs, return 0.
Example:
<?php // 建立与MySQL 数据库的连接$conn = mysql_connect("localhost", "username", "password"); if (!$conn) { die("连接失败: " . mysql_error()); } // 选择数据库mysql_select_db("mydb", $conn); // 执行查询$result = mysql_query("SELECT * FROM users", $conn); // 获取结果集中字段的数量$numFields = mysql_num_fields($result); echo "结果集中的字段数量为:" . $numFields; // 关闭连接mysql_close($conn); ?>
In the above example, we first establish a connection to the MySQL database and then select the database to operate on. Next, we execute a SELECT query statement and store the result in the $result
variable. Finally, we use mysql_num_fields()
function to get the number of fields in the result set and output it to the browser.
Please note that since PHP 7 has abandoned the mysql_
function, it is recommended to use the mysqli or PDO extension instead.