php - 为什么 mysqli_fetch_fields() 给我的结果不准确?

标签 php mysql mysqli

length结果来自 mysqli_fetch_fields()给我的结果不准确。

MySQL > DESCRIBE Notices;

Field     Type               Null     Key     Default     Extra
Id        int(10) unsigned   NO       PRI     (NULL)      auto_increment
UserId    int(10) unsigned   NO       MUL     (NULL)
Title     char(255)          NO               (NULL)
Name      char(255)          NO               (NULL)
Summary   text               NO               (NULL)

但是,

class Db {
    public function exec($sql) {
        if (!$this->link) {
            $this->open();
        }
        if (($this->result = mysqli_query($this->link, $sql)) === false) {
            throw new Exception('<b>' . __METHOD__ . '</b>: ' . $sql . ': ' . mysqli_error($this->link));
        }
        return true;
    }
    public function field_info($table) {
        $sql = 'SELECT * FROM `' . $table . '` LIMIT 1';
        $this->exec($sql);
        return $this->field_info = $this->result->fetch_fields();
    }
}

$a  = $db->field_info('Notices');
print_r($a);

产生以下输出:

// Note: "max_length" is the maximum existing length in the result set,
// while "length" is the set maximum length in the table definition.

Array
(
    [0] => stdClass Object
        (
            [name] => Id
            [orgname] => Id
            [table] => Notices
            [orgtable] => Notices
            [def] =>
            [max_length] => 1
            [length] => 10
            [charsetnr] => 63
            [flags] => 49699
            [type] => 3
            [decimals] => 0
        )

    [1] => stdClass Object
        (
            [name] => UserId
            [orgname] => UserId
            [table] => Notices
            [orgtable] => Notices
            [def] =>
            [max_length] => 1
            [length] => 10
            [charsetnr] => 63
            [flags] => 53289
            [type] => 3
            [decimals] => 0
        )

    [2] => stdClass Object
        (
            [name] => Title
            [orgname] => Title
            [table] => Notices
            [orgtable] => Notices
            [def] =>
            [max_length] => 27
            [length] => 765
            [charsetnr] => 33
            [flags] => 4097
            [type] => 254
            [decimals] => 0
        )

    [3] => stdClass Object
        (
            [name] => Name
            [orgname] => Name
            [table] => Notices
            [orgtable] => Notices
            [def] =>
            [max_length] => 25
            [length] => 765
            [charsetnr] => 33
            [flags] => 4097
            [type] => 254
            [decimals] => 0
        )

    [4] => stdClass Object
        (
            [name] => Summary
            [orgname] => Summary
            [table] => Notices
            [orgtable] => Notices
            [def] =>
            [max_length] => 26
            [length] => 196605
            [charsetnr] => 33
            [flags] => 4113
            [type] => 252
            [decimals] => 0
        )
)

如果你看,你会发现 CHAR 的报告长度字段不匹配。 DESCRIBE给我 255 , 而 fetch_fields()给我 765 .为什么?

(如果您想知道,这个想法是为 maxlength 标签生成一个 <input> 属性。)

最佳答案

您的字段长度为 255 个字符,但报告的长度以字节为单位。

因为 MySQL 使用 3 bytes per character对于 UTF-8 归类,您会得到 255*3 = 765 作为结果。

关于php - 为什么 mysqli_fetch_fields() 给我的结果不准确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10430406/

相关文章:

php - 如何编写 php MySql 查询

mysql - 从具有一对多关系的 MYSQL 连接创建单行

php - Ajax 只在第一次工作

php - 如何在Mysql中实现Join来获取以下记录

php - Mysqli 绑定(bind) - 未知数量的参数

php - 仅针对mysql中的特定列获取不同的值

php - 如何获得每分钟存储的值的每小时平均值

php - 如何维护各国时间戳

javascript - Ajax函数一次只显示一个div,PHP,AJAX

MySQL比较两个表的行散列