【问题标题】:How to find columns name from the table with php如何使用php从表中查找列名
【发布时间】:2010-11-22 13:34:37
【问题描述】:

有谁知道如何在 php 中找到选定表的列名?

示例: 如果我有如下表呼叫客户:

cust_id    cust_name   cust_age
1          Alan        35      
2          Alex        52
3          Amy         15

当我选择客户表时,如何仅在 php 中获取列名(它将返回值是 cust_id、cust_name 和 cust_age)?

【问题讨论】:

标签: php sql


【解决方案1】:

如果你的数据库是 MySQL 那么你可以在 php 中使用 mysql_field_name() 和 mysql_fetch_field() 函数。

$res = mysql_query('select * from customer', $link);

echo mysql_field_name($res, 0); // print cust_id
echo mysql_field_name($res, 1); // print cust_name 
echo mysql_field_name($res, 2); // print cust_age

你可以查看这个http://php.net/manual/en/function.mysql-field-name.php

如果您想要有关字段的其他信息,请查看此内容 http://us2.php.net/manual/en/function.mysql-fetch-field.php

【讨论】:

  • 我试过了..但显示错误消息:警告:mysql_field_name():提供的资源不是有效的 MySQL 结果资源
  • @Jin Yong: 试试这个 --> $res = mysql_query('select * from customer');
  • 我尝试了 mysql_query('select * from customer') 和 mysql_query('select * from customer',$link)。同样的错误显示.. :(
【解决方案2】:

您可以使用 SQL:SHOW COLUMNS FROM customer;DESCRIBE customer;

【讨论】:

    【解决方案3】:

    SHOW COLUMNS 或使用information_schema - 后者更便携

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2017-12-13
      • 1970-01-01
      • 2023-04-01
      • 2020-10-08
      相关资源
      最近更新 更多