php mysql獲取表字段名稱和字段信息的三種方法
php mysql獲取表字段名稱和字段信息的三種方法
先給出本實(shí)例中使用的表的信息:
使用desc獲取表字段信息
php代碼如下:
<?php mysql_connect("localhost","root",""); mysql_select_db("test"); $query = "desc student"; $result = mysql_query($query); while($row=mysql_fetch_assoc($result)){ print_r($row); } ?>
運(yùn)行結(jié)果:
Array ( [Field] => student_id [Type] => int(4) [Null] => NO [Key] => PRI [Default] => [Extra] => auto_increment ) Array ( [Field] => student_name [Type] => varchar(50) [Null] => NO [Key] => [Default] => [Extra] => ) Array ( [Field] => class_id [Type] => int(4) [Null] => NO [Key] => [Default] => [Extra] => ) Array ( [Field] => total_score [Type] => int(4) [Null] => NO [Key] => [Default] => [Extra] => )
使用SHOW FULL FIELDS獲取表字段信息
php代碼如下:
<?php mysql_connect("localhost","root",""); mysql_select_db("test"); $query = "SHOW FULL COLUMNS FROM student"; $result = mysql_query($query); while($row=mysql_fetch_assoc($result)){ print_r($row); } ?>
運(yùn)行結(jié)果:
Array ( [Field] => student_id [Type] => int(4) [Collation] => [Null] => NO [Key] => PRI [Default] => [Extra] => auto_increment [Privileges] => select,insert,update,references [Comment] => ) Array ( [Field] => student_name [Type] => varchar(50) [Collation] => latin1_swedish_ci [Null] => NO [Key] => [Default] => [Extra] => [Privileges] => select,insert,update,references [Comment] => ) Array ( [Field] => class_id [Type] => int(4) [Collation] => [Null] => NO [Key] => [Default] => [Extra] => [Privileges] => select,insert,update,references [Comment] => ) Array ( [Field] => total_score [Type] => int(4) [Collation] => [Null] => NO [Key] => [Default] => [Extra] => [Privileges] => select,insert,update,references [Comment] => )
使用mysql_fetch_field方法獲取表字段信息
php代碼如下:
<?php mysql_connect("localhost","root",""); mysql_select_db("test"); $query = "SELECT * FROM student LIMIT 1"; $result = mysql_query($query); $fields = mysql_num_fields($result); for($count=0;$count<$fields;$count++) { $field = mysql_fetch_field($result,$count); print_r($field); } ?>
運(yùn)行結(jié)果如下:
stdClass Object ( [name] => student_id [table] => student [def] => [max_length] => 1 [not_null] => 1 [primary_key] => 1 [multiple_key] => 0 [unique_key] => 0 [numeric] => 1 [blob] => 0 [type] => int [unsigned] => 0 [zerofill] => 0 ) stdClass Object ( [name] => student_name [table] => student [def] => [max_length] => 5 [not_null] => 1 [primary_key] => 0 [multiple_key] => 0 [unique_key] => 0 [numeric] => 0 [blob] => 0 [type] => string [unsigned] => 0 [zerofill] => 0 ) stdClass Object ( [name] => class_id [table] => student [def] => [max_length] => 1 [not_null] => 1 [primary_key] => 0 [multiple_key] => 0 [unique_key] => 0 [numeric] => 1 [blob] => 0 [type] => int [unsigned] => 0 [zerofill] => 0 ) stdClass Object ( [name] => total_score [table] => student [def] => [max_length] => 3 [not_null] => 1 [primary_key] => 0 [multiple_key] => 0 [unique_key] => 0 [numeric] => 1 [blob] => 0 [type] => int [unsigned] => 0 [zerofill] => 0 )
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
php中mkdir()函數(shù)的權(quán)限問題分析
這篇文章主要介紹了php中mkdir()函數(shù)的權(quán)限問題分析,需要的朋友可以參考下2016-09-09淺析PHP頁面局部刷新功能的實(shí)現(xiàn)小結(jié)
本篇文章是對PHP頁面局部刷新功能的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php XPath對XML文件查找及修改實(shí)現(xiàn)代碼
php XPath對XML文件查找及修改實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-07-07php數(shù)組函數(shù)序列之a(chǎn)rray_key_exists() - 查找數(shù)組鍵名是否存在
array_key_exists() 函數(shù)判斷某個(gè)數(shù)組中是否存在指定的 key,如果該 key 存在,則返回 true,否則返回 false2011-10-10PHP跨時(shí)區(qū)(UTC時(shí)間)應(yīng)用解決方案
解決PHP跨時(shí)區(qū)應(yīng)用需要將將程序內(nèi)部時(shí)區(qū)設(shè)置為UTC時(shí)間.(UTC 也可以叫 GMT) 數(shù)據(jù)庫中存儲(chǔ)UTC時(shí)間等等,感興趣的朋友可以了解下2013-01-01