dongnunai3125 2009-09-25 22:49
浏览 50
已采纳

PHP函数,它返回某个表中所有列的名称,作为数组

I've tried this: http://be.php.net/manual/en/function.mysql-list-fields.php, but there's too much detail here.

What I'm looking for is a function that returns an array of strings. This array should only contain the names of all columns that a certain table has.

So for instance, if I have a table with these columns:

username | email | gender |age | married | number_of_children | street | province

I should get the same thing as if I did this:

$vars = array('username','email','gender','age','married','number_of_children','street','province');

Is there a PHP function already that can do this? Or shall I need some SQL statements of my own?

  • 写回答

3条回答 默认 最新

  • dongya2030 2009-09-25 22:57
    关注

    you could also do

    $query = mysql_query("SELECT * FROM `table`");
    $result = mysql_fetch_assoc($query);
    $keys = array_keys($result);
    

    $keys will now contain all of the column names because they were the array keys in the $result array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度