954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to convert string variable to BIGINT & also to keep leading zeros ??

Hello,
I need to convert a string variable to BIGINT and store it into the mysql database. I have used settype() but its removing my leading zero in the string.
For example,

$new_id1="12";
   $new_id2="345";
   $zero="0";
   $id="{$zero}{$zero}{$new_id1}{$new_id2}";
   settype($id,"float");
   echo $id;

output:
12345   // Instead of getting 0012345


The above code removes 00 (leading zeros).Can someone help me how to keep the zeros along with variable type as float??
Help would be appreciated
Thanks.

ramsri05
Newbie Poster
6 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Numeric datatypes will never have leading zeros. You could add a dummy number to the front and just remove with substr when outputting it.

$new_id1="12";
   $new_id2="345";
   $zero="0";
   $id="1{$zero}{$zero}{$new_id1}{$new_id2}";   
   settype($id,"float");
   echo $id; // outputs 10012345
   echo"";
   $id = substr($id,1);//remove dummy number
   echo $id; // outputs 0012345
buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

Thanks buddylee17,
Well, I wanted to add leading zero in the numeric datatype only. because I should insert it into MySql database BIGINT data field.
Is there any way to insert the numeric/string value with leading zero into MYSQL BIGINT field ? If I insert a string of "0123456" this BIGINT field removes leading 0 and showing only 123456. how to make it show this 0 without enabling unsigned zerofill option? HELP pls !!

ramsri05
Newbie Poster
6 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
Thanks buddylee17, Well, I wanted to add leading zero in the numeric datatype only. because I should insert it into MySql database BIGINT data field. Is there any way to insert the numeric/string value with leading zero into MYSQL BIGINT field ? If I insert a string of "0123456" this BIGINT field removes leading 0 and showing only 123456. how to make it show this 0 without enabling unsigned zerofill option? HELP pls !!

MySQL does not know about PHP datatypes. So an integer or a string in PHP looks the same to MySQL.

It only depends on whether you quote the value in the MySQL query or not.

If you quote a value, it is treated as a string. However, MySQL will automatically convert that to the type of the column.

I don't think you can place 0 in front of a bigint column unless you specify zerofill.

If you have phpmyadmin just play around with the values and storage type of the table to make sure.

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You