Returns the meta-data of a single field (column) in the result set, and outputs the 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 information of field "name" $fieldinfo=mysqli_fetch_field_direct($result,1);printf("Field name: %s" ,$fieldinfo->name);echo "<br>";printf("Data table: %s",$fieldinfo->table);echo "<br>";printf("Maximum length: %d",$fieldinfo->max_length);// Release the result set mysqli_free_result($result);}mysqli_close($ con);?>
The mysqli_fetch_field_direct() function obtains the meta-data of a single field (column) from the result set and returns it as an object.
mysqli_fetch_field_direct( result,fieldnr ) ;
parameter | describe |
---|---|
result | Required. Specifies the result set identifier returned by mysqli_query(), mysqli_store_result(), or mysqli_use_result(). |
fieldnr | Required. Specifies the field number. Must be between 0 and number of fields - 1. |
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 - the default value for the field 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+ |