length
的{{1}}结果给了我不准确的结果。
MySQL> mysqli_fetch_fields()
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
属性。)
答案 0 :(得分:1)
您的字段长度为255 字符,但报告的长度为字节。
由于MySQL使用3 bytes per character进行UTF-8排序规则,因此结果会得到255 * 3 = 765
。
答案 1 :(得分:1)
length
/ charsetnr
输出中的mysqli_result::fetch_fields()
以及mysqli_fetch_fields()
值显然取决于客户端定义的默认字符集。
E.g。 charsetnr
代表33
代表UTF-8。确切地说,它实际上代表整理&#39; utf8_general_ci&#39;字符集&#39; utf8&#39;。可以通过执行以下查询来检索该信息:
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文档目前不包含该信息,因此我请求将其添加到bug 68573。
默认字符集也可以是defined within the server configuration described in another thread。
由于fetch_fields()
的这种行为非常混乱,我请求在bug 68574更改它。
答案 2 :(得分: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++;
}
答案 3 :(得分:-1)
我遇到了同样的问题,发现你需要使用: $ val-&GT;长度;
不是max_length,max_length是特定列中值的最大长度,而不是表def。
如果您希望表定义使用'length'。
$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