The function mysqli_result::$current_field() was introduced in PHP 5.3.0 and above. It is used to get the index of the current field.
Usage: mysqli_result::$current_field(): int|false
Parameters: This function has no parameters.
Return value: Returns the index of the current field, and returns false if it fails.
Example:
// 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 检查连接是否成功if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } // 执行查询语句$query = "SELECT id, name, email FROM users"; $result = $mysqli->query($query); // 获取当前字段的索引$fieldIndex = $result->current_field(); if ($fieldIndex !== false) { echo "当前字段的索引为: " . $fieldIndex; } else { echo "获取当前字段索引失败"; } // 释放结果集$result->close(); // 关闭数据库连接$mysqli->close();
In the example above, we first create a mysqli object and connect to the database. Then execute a query statement and store the result in the $result variable. Next, we use the current_field() function to get the index of the current field and print the result out. Finally, we release the result set and close the database connection.
Note that the current_field() function returns the index of the current field, counting from 0. If the index is failed, false will be returned.