Easy Tutorial
❮ Func Math Mt Getrandmax Filter Sanitize Special Chars ❯

PHP mysqli_fetch_field() Function

PHP MySQLi Reference Manual

Returns the next field (column) in the result set, then outputs each field 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 field information for all columns
    while ($fieldinfo = mysqli_fetch_field($result)) {

        printf("Field Name:     %s\n", $fieldinfo->name);
        echo "<br>";
        printf("Table:    %s\n", $fieldinfo->table);
        echo "<br>";
        printf("Max Length: %d\n", $fieldinfo->max_length);
        echo "<br>";
    }
    // Free result set
    mysqli_free_result($result);
}

mysqli_close($con);
?>

Definition and Usage

The mysqli_fetch_field() function fetches the next field (column) from the result set and returns it as an object.


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> def - reserved as the default value, currently always ""<br> db - database (added in PHP 5.3.6)<br> catalog - catalog name, always "def" (as of PHP 5.3.6)<br> max_length - maximum width of the field<br> length - width of the field as defined in the table<br> charsetnr - character set number of 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 Math Mt Getrandmax Filter Sanitize Special Chars ❯