简体   繁体   English

如何使用PHP从MySQL显示字段名称

[英]How to display field name from MySQL with PHP

I am trying to display field names from a MySQL query using PHP. 我正在尝试使用PHP从MySQL查询中显示字段名称。 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. 当我将字段名称打印到HTML表中时,我什么都没显示,我不确定这是否是我应该如何获取字段名称的方法。 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. mysqli_fetch_fields()返回对象数组,而不是对象。

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. 或在原始代码中使用函数mysqli_fetch_field()的单数形式。

For more information on how mysqli_fetch_fields work, check out the official documentation: http://php.net/manual/en/mysqli-result.fetch-fields.php 有关mysqli_fetch_fields如何工作的更多信息,请查看官方文档: http ://php.net/manual/en/mysqli-result.fetch-fields.php

It is better to use mysql_fetch_array() 最好使用mysql_fetch_array()

while( $data = mysql_fetch_array($result))
{
    echo "<th>$data['name']</th>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM