/    Sign up×
Community /Pin to ProfileBookmark

Trailing zero’s not showing in int field

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

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — 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:[code=php]$old_format = "545";
$new_format = substr("00000".$old_format,-5);
echo $new_format;
//output "00545"[/code]
Copy linkTweet thisAlerts:
@Typhoon101authorFeb 14.2007 — Hmm. The number of leading zeros could vary though. I guess i will have to change the field type.

thanks for your advice.
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — [code=php]<?php
echo leadzero(545,5);

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

Success!

Help @Typhoon101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...