title: php sql query removes leading 0s
body: <p>I have code which is essentially</p>
  
  <pre><code>$query = mysql_query('SELECT `foo` FROM `Bar` WHERE id=1', $conn)
  $result = mysql_fetch_assoc($query)
  $val = $map[$result['foo']];
  </code></pre>
  
  <p>where the type of foo is <code>CHAR(2)</code>, and the value for id=1 is 07</p>
  
  <p>But the value returned is just 7, which causes problems when using this as an index to an associative array.</p>
  
  <p>PhpMyAdmin shows the correct value of 07</p>
  
  <p>I get the same result when using mysql_fetch_object too</p>
  
  <p>From comments: result of var_dump($result) is</p>
  
  <pre><code>array
    'foo' =&gt; string '7' (length=1)
  </code></pre>
  
  <p>and var_dump($map) is
      array
        '07' => string 'bar' (length=3)</p>
  
  <p>EDIT 2:
  I have just found an even bigger issue with this: Phone numbers starting with a 0 are also affected. There is no easy way (like str_pad suggested below) to fix this issue</p>
  
  <p>EDIT 3:
  The server is Windows 7 with xampp 1.7.7 and PHP 5.3.8
  The database is InnoDB with latin1_swedish_ci and COMPACT row format</p>
  
  <pre><code>CREATE TABLE `Bar` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `foo` char(2) DEFAULT NULL
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p>EDIT 4:
  <code>SELECT CONCAT("0", foo) FROM Bar WHERE id = 55</code> returns 07</p>