2

结果lengthmysqli_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>

4

4 回答 4

1

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

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

于 2012-05-03T11:39:59.047 回答
1

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

例如 a charsetnrof33代表 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');

当前的 PHP 文档mysqli_fetch_fields()不包含该信息,因此我要求将其添加到bug 68573中。

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

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

于 2014-12-09T10:57:47.200 回答
0

我划分结果:

$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++;
  }
于 2014-11-26T18:08:41.233 回答
-1

我有同样的问题,发现你需要使用: $val->length;

不是 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

于 2013-04-16T22:10:31.630 回答