25

I have a way to get the name of the columns of a table. It works fine but now I want to update to the new mysqli ? (I tried the mysqli_fetch_field but I don't know how to apply to this case and I am not sure if it is the wright option)

How to do the same with mysqli ? :

$sql = "SELECT * from myTable";
$result = mysql_query($sql,$con);
$id = mysql_field_name($result, 0);
$a = mysql_field_name($result, 1);

echo $id;
echo $a;
Nrc
  • 9,577
  • 17
  • 67
  • 114
  • 1
    If you are going to use mysqli, all functions need to be converted to it. You can't mix mysql and mysqli. – datasage Jan 31 '13 at 15:49
  • mysql_field_name is already ancient and unusable way. Why do you need them, by the way? – Your Common Sense Jan 31 '13 at 15:54
  • 1
    That is the question: how to convert all to the new mysqli – Nrc Jan 31 '13 at 16:03
  • What is your goal? What for do you need field names? – Your Common Sense Feb 01 '13 at 16:48
  • 1
    If you use `mysqli_fetch_assoc`, to fetch the data, you will get the field names in the array, so you wouldn't need to query them separately. Also, it's generally considered good practice to list the fields you want in the query; ie use `SELECT fieldname, fieldname, etc` rather than `SELECT *`. If you do that, you'll already know what fields are in the results, so you wouldn't need to check back for the field list at all. – Spudley Feb 10 '13 at 12:34
  • You can replace the `function mysql_field_name` to `mysqli_fetch_field_direct` – Siddiqui Noor Aug 24 '17 at 19:58

5 Answers5

44

This is the way to implement this missing function:

function mysqli_field_name($result, $field_offset)
{
    $properties = mysqli_fetch_field_direct($result, $field_offset);
    return is_object($properties) ? $properties->name : null;
}
José Carlos PHP
  • 1,417
  • 1
  • 13
  • 20
  • This is the correct answer according the deprecation notice in [the documentation](http://php.net/manual/en/function.mysql-field-name.php), but the original function returned False on failure, not null. Shouldn't you just forgo the ternary operator and do return $properties->name; ? – kloddant Apr 20 '16 at 20:02
  • 1
    Just change null to false. – José Carlos PHP Jun 18 '16 at 12:04
17

I'm not sure if there is a better way to do that, but I checked that this works to get just the name of the columns and is the new mysqli :

$result = mysqli_query($con, 'SELECT * FROM myTable');
while ($property = mysqli_fetch_field($result)) {
    echo $property->name;
}
Nrc
  • 9,577
  • 17
  • 67
  • 114
  • if you need both the data and the metadata, you will lose the data. Also, this loop will go over the entire table. Also, given you are fetching anyway, why not just fetch the assoc array and use its keys? – Your Common Sense Feb 24 '23 at 12:01
7

You can replace the function mysql_field_name to mysqli_fetch_field_directand use it like the following:

$colObj = mysqli_fetch_field_direct($result,$i);                            
$col = $colObj->name;
echo "<br/>Coluna: ".$col;
Siddiqui Noor
  • 5,575
  • 5
  • 25
  • 41
user3187518
  • 71
  • 1
  • 2
  • This should be the correct answer. It is almost identical to mysql_field_name, except you have to de-reference the name: $id = mysql_field_name($result, 0) => $id = mysqli_fetch_field_direct($result, 0) – Phreditor May 21 '20 at 00:07
1
$sql = "SELECT * FROM myTable LIMIT 10";
$ressult = $con->query($sql);
$rows = $result->fetch_all(MYSQLI_ASSOC);
$fields = array_keys($rows[0] ?? []);
echo json_encode($fields);

Can return empty array if query returned no rows.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Sorry, I'm new in php and I don't understand your answer. How can I pick up just the title of the field, like in the example I give. – Nrc Feb 01 '13 at 16:39
1

This is another easy way to print each field's name, table, and max length

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
{
   // Get field information for all fields
   while ($fieldinfo=mysqli_fetch_field($result))
   {
      printf("Name: %s\n",$fieldinfo->name);
      printf("Table: %s\n",$fieldinfo->table);
      printf("max. Len: %d\n",$fieldinfo->max_length);
   }
   // Free result set
   mysqli_free_result($result);
}
A.A Noman
  • 5,244
  • 9
  • 24
  • 46