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

Reply

Join Date: Jul 2009
Posts: 6
Reputation: ramsri05 is an unknown quantity at this point 
Solved Threads: 0
ramsri05 ramsri05 is offline Offline
Newbie Poster

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

 
0
  #1
Aug 15th, 2009
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,

  1. $new_id1="12";
  2. $new_id2="345";
  3. $zero="0";
  4. $id="{$zero}{$zero}{$new_id1}{$new_id2}";
  5. settype($id,"float");
  6. echo $id;
  7.  
  8. output:
  9. 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.
Last edited by ramsri05; Aug 15th, 2009 at 4:34 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

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

 
0
  #2
Aug 15th, 2009
Numeric datatypes will never have leading zeros. You could add a dummy number to the front and just remove with substr when outputting it.
  1. $new_id1="12";
  2. $new_id2="345";
  3. $zero="0";
  4. $id="1{$zero}{$zero}{$new_id1}{$new_id2}";
  5. settype($id,"float");
  6. echo $id; // outputs 10012345
  7. echo"<br>";
  8. $id = substr($id,1);//remove dummy number
  9. echo $id; // outputs 0012345
Last edited by buddylee17; Aug 15th, 2009 at 10:46 am.
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 6
Reputation: ramsri05 is an unknown quantity at this point 
Solved Threads: 0
ramsri05 ramsri05 is offline Offline
Newbie Poster

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

 
0
  #3
Aug 15th, 2009
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 !!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

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

 
0
  #4
Aug 16th, 2009
Originally Posted by ramsri05 View Post
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC