length
的结果给了我不准确的结果。
MySQL>mysqli_fetch_fields()
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
)
)
如果您查看,您将看到
DESCRIBE Notices;
字段的报告长度不匹配。CHAR
给我DESCRIBE
,而255
给我fetch_fields()
。为什么?(如果您想知道,这个想法是为
765
标记生成一个maxlength
属性。) 最佳答案
字段长度为255个字符,但报告的长度以字节为单位。
由于MySQL使用3 bytes per character进行UTF-8排序,因此得到255*3=765
。