2012-05-03 48 views
2

length结果mysqli_fetch_fields()给我的结果不准确。为什么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>标签。)

回答

1

你的字段长度为255个字符,但报告的长度是字节

由于MySQL使用3 bytes per character进行UTF-8归类,因此得到255 * 3 = 765作为结果。

+0

它在哪里说长度是以字节为单位? [mysqli_fetch_fields'的文档](http://php.net/manual/en/mysqli-result.fetch-fields.php#refsect1-mysqli-result.fetch-fields-returnvalues)只会说“字段,如表中定义的那样。“你也只能在Windows上得到不正确的结果。当你在Linux上运行相同的脚本时,你会得到预期的结果,即'255'。 –

+0

@SebastianZartner。不。我发现PHP和MySQL都在Linux上运行时遇到了麻烦。 (在Ubuntu上都是这样,而在CentOS上也是这样) – TRiG

+0

对,上面的评论是我昨天的看法。尽管您可以在下面的答案中看到它,但它实际上与操作系统无关,而是通过默认的客户端字符集进行定义。 –

-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; 

} 

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

+0

是的。在我的问题中看到代码中的评论,这已经表明了这一点。 – TRiG

+0

啊没有注意到,由于水平滚动balk ... – stevesrs

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++; 
    } 
1

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

E.g. 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文档目前不包括这些信息,所以我要求将其添加到bug 68573

默认字符集也可以是defined within the server configuration,如described in another thread

由于fetch_fields()的这种行为相当混乱,我要求将其更改为bug 68574

相关问题