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