Easy Tutorial
❮ Func Misc Strip Whitespace Func Mysqli Fetch Field Direct ❯

PHP mysqli_fetch_fields() Function

PHP MySQLi Reference Manual

Returns an array of objects representing the fields (columns) in a result set, and then outputs each field's name, table, and maximum length:

<?php 
// Assume database username: root, password: 123456, database: tutorialpro 
$con = mysqli_connect("localhost", "root", "123456", "tutorialpro"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$sql = "SELECT name, url FROM websites ORDER BY alexa";

if ($result = mysqli_query($con, $sql))
{
    // Get information about all fields
    $fieldinfo = mysqli_fetch_fields($result);
    foreach ($fieldinfo as $val)
    {
        printf("Field Name: %s", $val->name);
        echo "<br>";
        printf("Table: %s", $val->table);
        echo "<br>";
        printf("Maximum Length: %d", $val->max_length);
        echo "<br>";
    }
    // Free the result set
    mysqli_free_result($result);
}

mysqli_close($con);
?>

Definition and Usage

The mysqli_fetch_fields() function returns an array of objects representing the fields (columns) in a result set.


Syntax

Parameter Description
result Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result(), or mysqli_use_result().

Technical Details

Return Value: Returns an object containing field definition information. Returns FALSE if no information is available. The object has the following properties: name - Column name<br> orgname - Original column name (if an alias was specified)<br> table - Table name<br> orgtable - Original table name (if an alias was specified)<br> max_length - Maximum width of the field<br> length - Width of the field as specified in the table definition<br> charsetnr - Character set number for the field<br> flags - Bit flags for the field<br> type - Data type used for the field<br> decimals - Number of decimals for integer fields
PHP Version: 5+
--- ---
❮ Func Misc Strip Whitespace Func Mysqli Fetch Field Direct ❯