如何使用mysqli函数获取结果中指定字段的名称

function table($sql,$border)
{

    $con = mysqli_connect("localhost","root","","dbit36");
    $resource = mysqli_query($con,$sql);

    echo"<table border=0>";

    for($i = 0; $i < mysqli_num_fields($resource); $i++)
    {

    echo "<td style='border:".$border."1px dotted;'><b><font color='990099'size='10'>".mysql_field_name( $resource, $i )."</b></font></td>";

    }

    echo "</tr>";
    while($row = mysqli_fetch_array($resource))
    {

            echo "<tr>";

        for($i = 0; $i < mysqli_num_fields($resource); $i++)
        {

            echo "<td style='border:".$border."px ridge;'>".$row[$i]."</td>";

        }

        echo "</tr>";

    }

    echo "</table>";

}

I know that using mysql function are deprecated in php 5.5+ versions thats why my question is there any mysqli function that can get the name of the specified field in a result?

By using mysqli_fetch_assoc instead of mysqli_fetch_array Then you can select any field in the table you're fetching by typing $row['field_name']

Reference: http://php.net/manual/en/mysqli-result.fetch-assoc.php

mysqli_fetch_fields is required function to get name of fields.

http://php.net/manual/en/mysqli-result.fetch-fields.php

Try the below one. This is an example, apply your required logic.

$resource = mysqli_query($con,$sql);
while ($fn = mysqli_fetch_field($resource)) 
    { 
        echo $fn->name;     //echo clumn name
        echo $fn->table;    //tanle name etc
    } 

Use foreach inside while loop if you really required it.