Home >Backend Development >PHP Tutorial >Is there an easier way to get the field name in thinkPHP?

Is there an easier way to get the field name in thinkPHP?

WBOY
WBOYOriginal
2016-07-06 13:53:551173browse

thinkphp 3.2

<code>$use=D('classone');

$db=$use->select(5);

$arr = $db[0] ? $db[0] : '';

foreach($arr as $key => $value)
{
       $arrKey[] =  $key;

}</code>

Here, I got the name of the field into an array $arrKey, but I think this method is stupid. There should be a way to get it directly. Is there any???

Reply content:

thinkphp 3.2

<code>$use=D('classone');

$db=$use->select(5);

$arr = $db[0] ? $db[0] : '';

foreach($arr as $key => $value)
{
       $arrKey[] =  $key;

}</code>

Here, I got the name of the field into an array $arrKey, but I think this method is stupid. There should be a way to get it directly. Is there any???

tp5

<code>/**
     * 获取数据表信息
     * @access public
     * @param string $tableName 数据表名 留空自动获取
     * @param string $fetch 获取信息类型 包括 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, ',')) {
            // 多表不获取字段信息
            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) {
                // 记录字段类型
                $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)) {
                // 设置主键
                $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];
    }</code>

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn