开发者

Zip Code missing leading 0 when retrieved from mySQL tables

开发者 https://www.devze.com 2023-03-05 11:22 出处:网络
I am storing 5 digit zip codes in mySQL tables as CHAR(5). However when a zip code with leading zeroes (ie. 02138) is retrieved from the tables, it becomes 2138. Since it is being stored in the tables

I am storing 5 digit zip codes in mySQL tables as CHAR(5). However when a zip code with leading zeroes (ie. 02138) is retrieved from the tables, it becomes 2138. Since it is being stored in the tables as 02138 (checked with phpmyad开发者_如何学Pythonmin), it has to do with PHP stripping off the leading 0? How can I make sure I retain the leading 0?

I'm using quite a long SQL query string using activerecords in codeigniter.

foreach($q as $row){
        /// bunch of code that adds more elements to the $row array

        $data['rows'][] = $row;
    }


str_pad($zip, 5, 0, STR_PAD_LEFT);


What you need is MySQL ZEROFILL. Specify the column as zerofill.


display it as text instead of as a number


As a quick-n-dirty workaround see @tandu version. But here you should find the root of the problem. What happens is clear -- somewhere PHP converts string "012345" into int 12345. So now you should somehow find, who do so and when. var_dump values and see if they string or int.

May be somewhere in your framework you specify column type as numeric, but it actually char in your DB.

0

精彩评论

暂无评论...
验证码 换一张
取 消