houssam_ballout Posted February 11, 2010 Share Posted February 11, 2010 Hello I had a mysql table with php pages related to it, When I insert 033333 , it will take 33333, which type should I put in order to preserve the leading 0? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/ Share on other sites More sharing options...
PravinS Posted February 11, 2010 Share Posted February 11, 2010 User VARCHAR() data type Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010682 Share on other sites More sharing options...
houssam_ballout Posted February 11, 2010 Author Share Posted February 11, 2010 well, should I need to do any trick in the php page, cause I had changed it in the mysql, Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010683 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 edit your table structure use integers type and just make sure "ZEROFILL" is checked Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010684 Share on other sites More sharing options...
houssam_ballout Posted February 11, 2010 Author Share Posted February 11, 2010 well, in the php page, if I echo the sql statement, it displays the correct one but when I look at the database, it won't work if I put INT (10), with Zerofill, if I put 3 numbers, it will put 7 zeros next to it. Any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010687 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 change max available length INT (10) => int(4) then 123 will be 0123 '9' will be 0009 Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010691 Share on other sites More sharing options...
houssam_ballout Posted February 11, 2010 Author Share Posted February 11, 2010 well, I know but the numbering criteria here could vary I just want that mysql query takes the leading 0 Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010692 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 Can u explane what exactly is wrong where your system fails, why u need those zeros or u just want to see them on database and be happy that 0 are there? Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010693 Share on other sites More sharing options...
houssam_ballout Posted February 11, 2010 Author Share Posted February 11, 2010 well, my form had a field for a phone number, and so, the leading zeros are being removed Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010694 Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 well in this case realy just use varchar as pbs sugessted. or I dont' know maybe some math thrick can be used here for example use int(10) as datatype w/o zerofill then before storing phone number prefix it with for example 1 $phone = "1".$_POST['phone_number']; mysql_query("INSERT INTO table (phone) VALUES ($phone)"); and when u retrive data back from database just be sure to remove that first charecter number and only then echo it or what ever you planing to do with phone number... Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010698 Share on other sites More sharing options...
Adam Posted February 11, 2010 Share Posted February 11, 2010 There's no use in "math tricks" (as you put it) like that. Adding the 1 at the start would void any MySQL calculations, which is generally the greatest benefit of using number types. Just store the number as a string. Quote Link to comment https://forums.phpfreaks.com/topic/191755-insert-0-in-a-db-table/#findComment-1010700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.