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: CODERCTO $con=mysqli_connect("localhost","root","123456","CODERCTO"); 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 the field information of all columns while ($fieldinfo = mysqli_fetch_field($result)) { printf("Field name: %s n", $fieldinfo->name); echo "<br>"; printf("Data table: %sn", $fieldinfo->table); echo "<br>"; printf("Maximum length: %dn", $fieldinfo->max_length); echo "<br>"; } // Release the result set mysqli_free_result($result);}mysqli_close($con );?>
The mysqli_fetch_field() function gets the next field (column) from the result set and returns it as an object.
mysqli_fetch_field( result ) ;
parameter | describe |
---|---|
result | Required. Specifies the result set identifier returned by mysqli_query(), mysqli_store_result(), or mysqli_use_result(). |
Return value: | Returns an object containing field definition information. Returns FALSE if no information is available. This object has the following properties: name - column name orgname - the original column name (if an alias is specified) table - table name orgtable - the original table name (if an alias is specified) def - reserved as default, currently always "" db - database (new in PHP 5.3.6) catalog - the catalog name, always "def" (since PHP 5.3.6) max_length - the maximum width of the field length - the field width specified in the table definition charsetnr - the character set number of the field flags - the bit flags of the field type - the data type used for the field decimals - integer field, number of digits after decimal point |
---|---|
PHP version: | 5+ |