I am trying to display field names from a MySQL query using PHP. I'm not getting anything to display when I print the field name into an HTML table and I'm not sure if this is how I should be going about getting the field names. Here is my code:
$i = 0;
while( $i < mysqli_num_fields($result)){
$field_names = mysqli_fetch_fields($result);
echo "<th>$field_names->name</th>";
$i++;}
mysqli_fetch_fields()
returns an array of objects, not an object.
Try replacing the above mentioned code, with the following, this should work:
$fields=mysqli_fetch_fields($result);
foreach ($fields as $field) echo "<th>$field->name</th>";
or use the singular version of the function mysqli_fetch_field()
in your original code.
For more information on how mysqli_fetch_fields work, check out the official documentation: http://php.net/manual/en/mysqli-result.fetch-fields.php
It is better to use mysql_fetch_array()
while( $data = mysql_fetch_array($result))
{
echo "<th>$data['name']</th>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.