title: Why is mysqli_fetch_fields() giving me inaccurate results?
body: <p>The <code>length</code> result from <code>mysqli_fetch_fields()</code> is giving me inaccurate results.</p>
  
  <p>MySQL > <code>DESCRIBE Notices;</code></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>However,</p>
  
  <pre><code>class Db {
      public function exec($sql) {
          if (!$this-&gt;link) {
              $this-&gt;open();
          }
          if (($this-&gt;result = mysqli_query($this-&gt;link, $sql)) === false) {
              throw new Exception('&lt;b&gt;' . __METHOD__ . '&lt;/b&gt;: ' . $sql . ': ' . mysqli_error($this-&gt;link));
          }
          return true;
      }
      public function field_info($table) {
          $sql = 'SELECT * FROM `' . $table . '` LIMIT 1';
          $this-&gt;exec($sql);
          return $this-&gt;field_info = $this-&gt;result-&gt;fetch_fields();
      }
  }
  
  $a  = $db-&gt;field_info('Notices');
  print_r($a);
  </code></pre>
  
  <p>Produces the following output:</p>
  
  <pre class="lang-none prettyprint-override"><code>// 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] =&gt; stdClass Object
          (
              [name] =&gt; Id
              [orgname] =&gt; Id
              [table] =&gt; Notices
              [orgtable] =&gt; Notices
              [def] =&gt;
              [max_length] =&gt; 1
              [length] =&gt; 10
              [charsetnr] =&gt; 63
              [flags] =&gt; 49699
              [type] =&gt; 3
              [decimals] =&gt; 0
          )
  
      [1] =&gt; stdClass Object
          (
              [name] =&gt; UserId
              [orgname] =&gt; UserId
              [table] =&gt; Notices
              [orgtable] =&gt; Notices
              [def] =&gt;
              [max_length] =&gt; 1
              [length] =&gt; 10
              [charsetnr] =&gt; 63
              [flags] =&gt; 53289
              [type] =&gt; 3
              [decimals] =&gt; 0
          )
  
      [2] =&gt; stdClass Object
          (
              [name] =&gt; Title
              [orgname] =&gt; Title
              [table] =&gt; Notices
              [orgtable] =&gt; Notices
              [def] =&gt;
              [max_length] =&gt; 27
              [length] =&gt; 765
              [charsetnr] =&gt; 33
              [flags] =&gt; 4097
              [type] =&gt; 254
              [decimals] =&gt; 0
          )
  
      [3] =&gt; stdClass Object
          (
              [name] =&gt; Name
              [orgname] =&gt; Name
              [table] =&gt; Notices
              [orgtable] =&gt; Notices
              [def] =&gt;
              [max_length] =&gt; 25
              [length] =&gt; 765
              [charsetnr] =&gt; 33
              [flags] =&gt; 4097
              [type] =&gt; 254
              [decimals] =&gt; 0
          )
  
      [4] =&gt; stdClass Object
          (
              [name] =&gt; Summary
              [orgname] =&gt; Summary
              [table] =&gt; Notices
              [orgtable] =&gt; Notices
              [def] =&gt;
              [max_length] =&gt; 26
              [length] =&gt; 196605
              [charsetnr] =&gt; 33
              [flags] =&gt; 4113
              [type] =&gt; 252
              [decimals] =&gt; 0
          )
  )
  </code></pre>
  
  <p>If you look, you'll see that the reported lengths of the <code>CHAR</code> fields do not match. <code>DESCRIBE</code> is giving me <code>255</code>, while <code>fetch_fields()</code> is giving me <code>765</code>. Why?</p>
  
  <p>(The idea, in case you're wondering, is to produce a <code>maxlength</code> attribute for <code>&lt;input&gt;</code> tags.)</p>