簡體   English   中英

如何使用PHP從MySQL顯示字段名稱

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

我正在嘗試使用PHP從MySQL查詢中顯示字段名稱。 當我將字段名稱打印到HTML表中時,我什么都沒顯示,我不確定這是否是我應該如何獲取字段名稱的方法。 這是我的代碼:

$i = 0;
while( $i < mysqli_num_fields($result)){
$field_names = mysqli_fetch_fields($result);
echo "<th>$field_names->name</th>";
$i++;}

mysqli_fetch_fields()返回對象數組,而不是對象。

嘗試用以下代碼替換上面提到的代碼,這應該可以工作:

$fields=mysqli_fetch_fields($result);
foreach ($fields as $field) echo "<th>$field->name</th>";

或在原始代碼中使用函數mysqli_fetch_field()的單數形式。

有關mysqli_fetch_fields如何工作的更多信息,請查看官方文檔: http ://php.net/manual/en/mysqli-result.fetch-fields.php

最好使用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