English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_fetch_fields() function returns an array of objects representing the fields (columns) in the result set.
PHP result object (belongs to the mysqli_result class) represents the MySQL result returned by SELECT or DESCRIBE or EXPLAIN query.
The function mysqli_fetch_fields is used to: accept a result object as a parameter, and return an array of objects, each representing a field in the result.
mysqli_fetch_fields($result);
Serial number | Parameters and descriptions |
---|---|
1 | result (required) This is the identifier for the result object. |
The PHP mysqli_fetch_fields() function returns an array of objects, each of which contains the definition information of the fields in the given result. If there is no information, this function will returnFALSE.
The object returned by the array contains the following properties $ minus;.
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 - Default value of this field
max_length - Maximum width of the field
length - Field width specified in table definition
charsetnr - Field charset number
flags - Field bit flags
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 example demonstratesmysqli_fetch_fields()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("Create 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("Insert record.....\n"); //Retrieve the content of the table $res = mysqli_query($con, "SELECT * FROM myplayers"); //Get all fields $info = mysqli_fetch_fields($res); foreach ($info as $ele) { print("ID: ".$ele->name."\n"); print("First_Name: ".$ele->table."\n"); print("Last_Name: ".$ele->max_length."\n"); print("Place_Of_Birth: ".$ele->charsetnr."\n"); print("Country: ".$ele->flags."\n"); print("Country: ".$ele->type."\n"); print("\n"); } //End statement mysqli_free_result($res); //Close connection mysqli_close($con); ?>
Output result
Create table..... Insert record..... ID: ID First_Name: myplayers Last_Name: 1 Place_Of_Birth: 63 Country: 32768 Country: 3 ID: First Name First_Name: myplayers Last_Name: 8 Place_Of_Birth: 33 Country: 0 Country: 253 ID: Last Name First_Name: myplayers Last_Name: 10 Place_Of_Birth: 33 Country: 0 Country: 253 ID: Place Of Birth First_Name: myplayers Last_Name: 8 Place_Of_Birth: 33 Country: 0 Country: 253 ID: Country First_Name: myplayers Last_Name: 11 Place_Of_Birth: 33 Country: 0 Country: 253
In the object-oriented style, the syntax of this function is$result-> fetch_fields();。The following 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("Create 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(); //Get all rows as an array $info = $result->fetch_fields(); foreach ($info as $ele) { print("ID: ".$ele->name."\n"); print("First_Name: ".$ele->table."\n"); print("Last_Name: ".$ele->max_length."\n"); print("Place_Of_Birth: ".$ele->charsetnr."\n"); print("Country: ".$ele->flags."\n"); print("Country: ".$ele->type."\n"); print("\n"); } //End statement $stmt->close(); //Close connection $con->close(); ?>