我正在尝试显示数据库中的列名,并且能够通过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);
}
最佳答案
尝试这种方式:
// 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
关于php - mysqli_fetch_field()多次输出字段名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20118323/