MySQL query to get column names?

前端 未结 21 2170
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 01:56

I\'d like to get all of a mysql table\'s col names into an array in php?

Is there a query for this?

21条回答
  •  心在旅途
    2020-11-22 02:41

    An old PHP function "mysql_list_fields()" is deprecated. So, today the best way to get names of fields is a query "SHOW COLUMNS FROM table_name [LIKE 'name']". So, here is a little example:

    $fields = array();
    $res=mysql_query("SHOW COLUMNS FROM mytable");
    while ($x = mysql_fetch_assoc($res)){
      $fields[] = $x['Field'];
    }
    foreach ($fields as $f) { echo "
    Field name: ".$f; }

提交回复
热议问题