Is there a simpler way for thinkPHP to get the field name?

Source: Internet
Author: User
Thinkphp3.2 {code ...} in this case, I get the field name into an array $ arrKey, but I think this method is stupid and there should be a direct way to get it. is there any ??? Thinkphp 3.2

$use=D('classone');$db=$use->select(5);$arr = $db[0] ? $db[0] : '';foreach($arr as $key => $value){       $arrKey[] =  $key;}

In this case, I get the field name into an array $ arrKey, but I think this method is stupid and there should be a direct way to get it. is there any ???

Reply content:

Thinkphp 3.2

$use=D('classone');$db=$use->select(5);$arr = $db[0] ? $db[0] : '';foreach($arr as $key => $value){       $arrKey[] =  $key;}

In this case, I get the field name into an array $ arrKey, but I think this method is stupid and there should be a direct way to get it. is there any ???

Tp5?

/*** Get data table information ** @ access public * @ param string $ tableName automatic acquisition of blank data table names * @ param string $ fetch information types include fields type bind pk * @ return mixed */public function getTableInfo ($ tableName = '', $ fetch = '') {static $ _ info = []; if (! $ TableName) {$ tableName = $ this-> getTable ();} if (is_array ($ tableName) {$ tableName = key ($ tableName )?: Current ($ tableName);} if (strpos ($ tableName, ',') {// do not obtain field information for multiple tables return false ;} $ guid = md5 ($ tableName); if (! Isset ($ _ info [$ guid]) {$ info = $ this-> connection-> getFields ($ tableName); $ fields = array_keys ($ info ); $ bind = $ type = []; foreach ($ info as $ key => $ val) {// record field type $ type [$ key] = $ val ['type']; if (preg_match ('/(int | double | float | decimal | real | numeric | serial)/is', $ val ['type']) {$ bind [$ key] = PDO: PARAM_INT;} elseif (preg_match ('/bool/is', $ val ['type']) {$ bind [$ key] = PDO: PARAM_BOOL;} else {$ Bind [$ key] = PDO: PARAM_STR;} if (! Empty ($ val ['primary']) {$ pk [] = $ key ;}} if (isset ($ pk )) {// set the primary key $ pk = count ($ pk)> 1? $ Pk: $ pk [0];} else {$ pk = null;} $ result = ['fields' => $ fields, 'type' => $ type, 'bind' => $ bind, 'PK' => $ pk]; $ _ info [$ guid] = $ result;} return $ fetch? $ _ Info [$ guid] [$ fetch]: $ _ info [$ guid];}

Summary of thinkphp quick query getBy, getField, getFieldBy usage and use case http://baijunyao.com/article/59

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.