English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_fetch_field() function returns the next field in the result set
PHP result object (belongs to the mysqli_result class) represents the MySQL result returned by SELECT or DESCRIBE or EXPLAIN queries.
The function of mysqli_fetch_field is to: accept a result object as a parameter and return the next column in the form of an object/Field definition information.
mysqli_fetch_field($result);
Serial number | Parameters and descriptions |
---|---|
1 | result (required) This is an identifier representing the result object. |
The PHP mysqli_fetch_field() function returns an object that contains the definition information of the field in the given result. If there is no information, this function will returnFALSE.
The returned object contains the following properties:
name - Column name
orgname - Original column name (if an alias is specified)
table - Table name
orgtable - Original table name (if an alias is specified)
def - reserved as the default value, currently always set to ""
db - Database (in PHP 5.3.6 added)
catalog - Catalog name, always set to "def" (since PHP 5.3.6 to)
max_length - Maximum width of the field
length - Field width specified in table definition
charsetnr - Character set number of the field
flags - Bit flags of the field
type - Data type used for the field
decimals - integer field, number of decimal places
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_fetch_field()Function usage (procedural style)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Creating table.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); print("Inserting record.....\n"); //Retrieve the content of the table $res = mysqli_query($con, "SELECT * FROM myplayers"); //Get field while($info = mysqli_fetch_field($res)){ //Current field $currentfield = mysqli_field_tell($res); print("Current field: " . $currentfield . "\n"); print("Field name: " . $info->name."\n"); print("Field type: " . $info->type."\n"); } //End statement mysqli_free_result($res); //Close connection mysqli_close($con); ?>
Output result
Create table..... Insert record..... Current field: 1 Field name: ID Field type: 3 Current field: 2 Field name: First_Name Field type: 253 Current field: 3 Field name: Last_Name Field type: 253 Current field: 4 Field name: Place_Of_Birth Field type: 253 Current field: 5 Field name: Country Field type: 253
In the object-oriented style, the syntax of this function is$result-> fetch_field;Here is an example of this function in an object-oriented style;
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27); print("Creating table.....\n"); $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; //Execute statement $stmt->execute(); //Search results $result = $stmt->get_result(); //Current field while($info = $result->fetch_field()){ $field = $result->current_field; print("Current field: " . $field . "\n"); print("Field name: " . $info->name."\n"); } //End statement $stmt->close(); //Close connection $con->close(); ?>