mysqli_fetch_field()多次输出字段名称

时间:2013-11-21 10:26:31

标签: php mysql

我正在尝试显示我的数据库中的列名,我可以使用mysqli_fetch_field()函数执行此操作,但我的问题是,当我只想输出每个列时,它会多次输出列名列名一次。我将$field_info变量设置为该函数,该变量采用参数$result,该参数设置为我的查询,所有这些都放入我的while循环中。我有一种感觉,这可能是由我的while循环引起的?但我不太确定。

$query = "SELECT * FROM bodyshops_master_network";

if ($result = $mysqli->query($query))
{

// print the column names as the headers of a table 
while ($field_info = mysqli_fetch_field($result))
{
    printf("postcodes_covered %s\n", $field_info->postcodes_covered);
    printf("dealer_code %s\n", $field_info->dealer_code);
    printf("dealer_name %s\n", $field_info->dealer_name);
    printf("bodyshop_id %s\n", $field_info->bodyshop_id);
    printf("bodyshop_name %s\n", $field_info->bodyshop_name);
    printf("address1 %s\n", $field_info->address1);
    printf("address2 %s\n", $field_info->address2);
    printf("address3 %s\n", $field_info->address3);
    printf("address4 %s\n", $field_info->address4);
    printf("address5 %s\n", $field_info->address5);
    printf("postcode %s\n", $field_info->postcode);
    printf("BO_tel %s\n", $field_info->BO_tel);
    printf("BO_email %s\n", $field_info->BO_email);
    printf("BC_name %s\n", $field_info->BC_name);
    printf("equity_contract %s\n", $field_info->equity_contract);

}

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

// making a query
$query = "SELECT * FROM bodyshops_master_network";

// firing a query and getting the resource in $result variable
$result = $mysqli->query($query);

$i = 0;

// loop all fields
while ($i < mysqli_field_count($result)) {

    // fetching field information. Index of the field is given by $i
    $meta = mysqli_fetch_field($result, $i);
    // display information for each field here
    .
    .
    //
    $i++;
}

参考 mysqli_field_count mysqli_fetch_field 官方文档