doutao1282 2011-04-28 21:32
浏览 128
已采纳

mysql中获取表的列名的语法是什么?

Can somebody please enlighten me on the use of SHOW COLUMNS specifically in regards to PHP/MySQL because I have not found anything of much use through search.

What I want to do is get all the column names out of a table, then use those to get the correct data from session to do an update.

what i have so far is:

function get_Columns($con, $table){

    $sql_statement = "SHOW COLUMNS FROM " . NAME_TABLE_BASE . $table ;

    $temp = mysql_query($sql_statement) or die(mysql(error)) ;

    return mysql_fetch_array($temp) ;
}
?>

The problem is this is only returning the first column and not all of them. What do I need to change? Also is there anything I can add to $sql_statement to only get the fields and not all the other info like key type etc?

  • 写回答

2条回答 默认 最新

  • dongzhan1570 2011-04-28 22:34
    关注

    SHOW COLUMNS ... is the oldschool method. For MySQL v5.x installs you can query the database's INFORMATION_SCHEMA database directly for anything related to the databases/tables/fields it's hosting:

    select COLUMN_NAME
    from INFORMATION_SCHEMA.COLUMNS
    where (TABLE_SCHEMA = 'your database') AND (TABLE_NAME = 'your table')
    

    There's many more fields with specific field data in that table as well, documented here.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀