PHP mysqli_result fetch_column() Method
The PHP mysqli_result::fetch_column() / mysqli_fetch_column() function is used to fetch one row of data from the result set and returns the 0-indexed column. Each subsequent call to this function will return the value from the next row within the result set, or false if there are no more rows.
Syntax
//Object-oriented style public mysqli_result::fetch_column(column) //Procedural style mysqli_fetch_column(result, column)
Parameters
result |
Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result(). |
column |
Optional. Specify the index number of the column which need to be retrieved from the row. If no value is supplied, the first column will be returned. |
Return Value
Returns a single column from the next row of a result set or false if there are no more rows.
Example: Object-oriented style
The example below shows the usage of mysqli_result::fetch_column() method.
<?php //establishing connection to the database $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: ". $mysqli->connect_error; exit(); } //getting query result from the database $sql = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age"; $result = $mysqli->query($sql); //closing the connection $mysqli->close(); //processing the data retrieved from the database //- fetching a single value from the second column while($Name = $result->fetch_column(1)) { printf("%s\n", $Name); } ?>
The output of the above code will be similar to:
Marry Kim John Adam
Example: Procedural style
The example below shows the usage of mysqli_fetch_column() function.
<?php //establishing connection to the database $mysqli = mysqli_connect("localhost", "user", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); exit(); } //getting query result from the database $sql = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age"; $result = mysqli_query($mysqli, $sql); //closing the connection mysqli_close($mysqli); //processing the data retrieved from the database //- fetching a single value from the second column while($Name = mysqli_fetch_column($result, 1)) { printf("%s\n", $Name); } ?>
The output of the above code will be similar to:
Marry Kim John Adam
❮ PHP MySQLi Reference