Click to See Complete Forum and Search --> : Trailing zero's not showing in int field


Typhoon101
02-14-2007, 04:24 AM
Hi. I have a MySQL table which contains an "int" type field. This will obviously only hold numbers. I have a problem displaying these numbers on a PHP page if the number starts with a zero. These seem to get cropped off.

for example, if the number in the table was 000545, the PHP page just shows 545.

Can anyone tell me how to get around this without having to convert the field type to Text.

thanks

NightShift58
02-14-2007, 04:33 AM
By definition, an integer doesn't have leading zeroes so you won't get MySQL around to doing it unless you change data types.

At the PHP level, one way to turn your data into a string with leading zeroes is:$old_format = "545";
$new_format = substr("00000".$old_format,-5);
echo $new_format;
//output "00545"

Typhoon101
02-14-2007, 04:35 AM
Hmm. The number of leading zeros could vary though. I guess i will have to change the field type.

thanks for your advice.

NightShift58
02-14-2007, 04:58 AM
<?php
echo leadzero(545,5);

function leadzero($pINT, $pLEN=5) {
return substr(str_repeat("0",$pLEN).$pINT),-$pLEN);
}
?>