php mysql擷取表欄位名稱和欄位資訊的三種方法_php技巧

來源:互聯網
上載者:User

php mysql擷取表欄位名稱和欄位資訊的三種方法

先給出本執行個體中使用的表的資訊:

使用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);  }?>

運行結果:

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);  }?>

運行結果:

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);  }?>

運行結果如下:

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)

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.