Fonction PHP mysqli_fetch_fields ()

Définition et utilisation

Un objet résultat PHP (de la classe mysqli_result) représente le résultat MySQL, retourné par les requêtes SELECT ou, DESCRIBE ou, EXPLAIN.

La fonction mysqli_fetch_fields () accepte un objet de résultat en tant que paramètre et renvoie un tableau d'objets représentant chacun un champ dans le résultat.

Syntaxe

mysqli_fetch_fields($result);

Paramètres

Sr. Non Paramètre et description
1

result(Mandatory)

Il s'agit d'un identifiant représentant un objet de résultat.

Valeurs de retour

La fonction PHP mysqli_fetch_fields () retourne un tableau d'objets où chaque objet contient des informations de définition d'un champ dans le résultat donné. Cette fonction renvoie FALSE en cas d'absence d'information.

Les objets du tableau retourné contiennent les propriétés suivantes $ minus;

  • name

  • orgname

  • table

  • orgtable

  • max_length

  • length

  • charsetnr

  • flags

  • type

  • decimals

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation de la fonction mysqli_fetch_fields () (dans un style procédural) -

<?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("Table Created.....\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("Record Inserted.....\n");

   //Retrieving the contents of the table
   $res = mysqli_query($con, "SELECT * FROM myplayers");

   //Fetching all the 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");
   }
   //Closing the statement
   mysqli_free_result($res);

   //Closing the connection
   mysqli_close($con);
?>

Cela produira le résultat suivant -

Table Created.....
Record Inserted.....
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

Exemple

Dans le style orienté objet, la syntaxe de cette fonction est $ result-> fetch_fields (); Voici l'exemple de cette fonction dans le style orienté objet $ minus;

<?php
   //Creating a 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("Table Created.....\n");

   $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';

   //Executing the statement
   $stmt->execute();

   //Retrieving the result
   $result = $stmt->get_result();

   //Fetching all the rows as arrays
   $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");
   }
   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>