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


Why is mysqli_fetch_fields() giving me inaccurate results?

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

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。为什么?

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

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

由于 MySQL 对 UTF-8 排序规则的每个字符使用 3 个字节,因此您将获得 255*3 = 765 作为结果。

mysqli_result::fetch_fields()/mysqli_fetch_fields()输出中的lengthcharsetnr值显然取决于客户端定义的默认字符集。

例如,33charsetnr代表 UTF-8。准确地说,它实际上代表字符集"utf8"的排序规则"utf8_general_ci"。可以通过执行此查询来检索该信息:

SELECT * FROM information_schema.collations;

可以通过以下查询检索可用字符集及其字符长度的列表:

SELECT * FROM information_schema.character_sets;

默认客户端字符集可以通过 PHP 调用 mysqli::set_charset()/mysqli_set_charset() 来更改。

例:

$dbHandle->set_charset('latin1');

mysqli_fetch_fields()的 PHP 文档目前不包含该信息,因此我请求将其添加到错误 68573 中。

默认字符集也可以在服务器配置中定义,如另一个线程中所述。

由于fetch_fields()的这种行为非常令人困惑,我要求在错误 68574 中更改它。

我将结果除法:

$result = mysqli_query($con,"SELECT * from tabla");
$campoTD = mysqli_fetch_fields($result);
  foreach ($campoTD as $campo){
    echo '<input maxlength='.number_format($campo[$i]->length / 3).' >';
    $i++;
  }

我遇到了同样的问题,发现你需要使用:$val>长度;

不是max_length,max_length是该特定列中值的最大长度,而不是表 def。

如果要使用表定义,请使用"长度"。

$q = $mysqli->query("select * from table");
$finfo = $q->fetch_fields();
foreach ($finfo as $val) {
    echo $val->length;
}

参考: http://php.undmedlibrary.org/manual/de/function.mysqli-fetch-fields.php