MySQL query to get column names?

前端 未结 21 2171
伪装坚强ぢ
伪装坚强ぢ 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:52

    Not sure if this is what you were looking for, but this worked for me:

    $query = query("DESC YourTable");  
    $col_names = array_column($query, 'Field');
    

    That returns a simple array of the column names / variable names in your table or array as strings, which is what I needed to dynamically build MySQL queries. My frustration was that I simply don't know how to index arrays in PHP very well, so I wasn't sure what to do with the results from DESC or SHOW. Hope my answer is helpful to beginners like myself!

    To check result: print_r($col_names);

提交回复
热议问题