mysql_field_name to the new mysqli

Nrc picture Nrc · Jan 31, 2013 · Viewed 59.4k times · Source

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;

Answer

José Carlos PHP picture José Carlos PHP · Dec 10, 2014

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;
}