Current Location: Home> Function Categories> mysqli_result::fetch_column

mysqli_result::fetch_column

(mysqli_fetch_column) Fetch a single column from the next row of a result set
Name:mysqli_result::fetch_column
Category:MySQLi
Programming Language:php
One-line Description:Get the value of a column from the result set

Function name: mysqli_result::fetch_column()

Applicable version: PHP 5 >= 5.3.0, PHP 7

Usage: The mysqli_result::fetch_column() function is used to get the value of a column from the result set. This function can only be used for SELECT query result sets executed using mysqli::query().

Example:

 // 假设已经建立了数据库连接$conn // 执行SELECT 查询$query = "SELECT name FROM users"; $result = $conn->query($query); // 检查查询结果是否为空if ($result->num_rows > 0) { // 获取第一列的值$column = $result->fetch_column(); // 输出列的值echo "Column value: " . $column; } else { echo "No results found."; } // 释放结果集$result->close(); // 关闭数据库连接$conn->close();

Notes:

  1. The mysqli_result::fetch_column() function can only be called on SELECT query result sets executed using mysqli::query(). If you try to call the function on a non-query result set, an error will be caused.
  2. This function can only get the value of one column per call. If you need to get the value of multiple columns, you need to call the function multiple times, or use other applicable methods to get it.
  3. Before calling mysqli_result::fetch_column(), you must first use the mysqli_result::num_rows property or other methods to check whether the result set is empty to avoid attempting to call the function on an empty result set to cause an error.
Similar Functions
Popular Articles