2012-05-03 2 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의보고 길이 필드가 일치하지 않습니다. DESCRIBE255 인 반면, fetch_fields()765이됩니다. 왜?

답변

1

귀하의 필드 길이가 255 개 문자이다 (당신이 궁금해하는 경우에 생각은. <input> 태그에 대한 maxlength 속성을 생산하는 것입니다)하지만,보고 된 길이는 바이트입니다.

MySQL은 UTF-8 데이터 정렬에 3 bytes per character을 사용하므로 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에서 실행되는 문제를보고있었습니다. (우분투와 둘 다 CentOS에서 다시.) – TRiG

+0

위의 설명은 어제의 나의 인식이었습니다. 비록 당신이 아래에 내 대답에서 볼 수 있듯이 실제로는 OS와 관련이 없지만 오히려 기본 클라이언트 문자 집합을 통해 정의됩니다. –

-1

나는 동일한 문제가있어서 다음을 사용해야 함을 알았습니다. $ val-> length;

not max_length, max_length는 테이블 def가 아닌 특정 열의 값의 최대 길이입니다.

테이블 정의를 원하면 '길이'를 사용하십시오.

$q = $mysqli->query("select * from table"); 

$finfo = $q->fetch_fields(); 

foreach ($finfo as $val) { 

    echo $val->length; 

} 

는 REF :

+0

예. 이미 그 점을 내 질문에 코드 주석을보십시오. – TRiG

+0

아 가로 스크롤 balk 때문에 그 통지하지 않았다 ... – stevesrs

1

분명 의해 정의 된 기본 문자 세트에 의존 length뿐만 아니라 mysqli_result::fetch_fields()/mysqli_fetch_fields() 출력에서 ​​charsetnr 값 고객.

예. charsetnr33이며 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으로 변경해야합니다.

관련 문제