Inserting number that starts with 0 leaves off the 0

jonofat
jonofat used Ask the Experts™
on
I am insterting a phone number in to a database and the number starts with 0. When I view the record it shows everything after the 0 but never shows the 0. I have changed the type in the database from integer to varchar to text and stll won't work!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
Welcome to the world of loosely typed languages.  You can prepend the letter 'A' to the string and insert it.  Then it will be kept as a string.  To remove the 'A', you can do something like this:

$str = str_replace('A', NULL, $str);

Author

Commented:
Hmm, okay. So what do I do for this?

<?php echo $row_Recordset1['contactno']; ?>

and also, users will be able to change their numbers to...

so, if they type 011 123 4567 is there no way to get it to store in the database like that?
Most Valuable Expert 2011
Top Expert 2016
Commented:
so, if they type 011 123 4567 is there no way to get it to store in the database like that?

There are a lot of complex interactions to get information from a client into the data base.  Some of these interactions go through PHP, where strings may get recast as numbers.  You might want to find the earliest interaction with the data (probably a client form gets submitted?) and make your string "safe" there.  Otherwise you may inadvertently encounter type juggling.

This problem extends beyond just strings.  Consider what happens when you try to do this with an array in PHP.

$array = array(1 => 'One', '0001' => 'LZ One');
var_dump($array);

You will discover that the array keys are loosely typed.
http://php.net/manual/en/types.comparisons.php
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Author

Commented:
Well, what I did is in the update record code, I just changed it to

($_POST['contactno'], "text"),

instead of ($_POST['contactno'], "int"),

and that worked. Is it acceptable?
Most Valuable Expert 2011
Top Expert 2016
Commented:
It's great as long as you don't use the contactno field in a way that allows type juggling when the numeric string is assigned to a variable.  You're using Dreamweaver, right?  You may want to be careful about what Dreamweaver does with your variables - check the input, the code and the output for consistency.

One of these days I may write an article about type juggling.

Author

Commented:
Thanks Ray. Yeah, an article about type juggling would be great!
Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for the points - it's a great question, and in particular it's one that I lost an entire morning to over the array index issues.  I wish PHP had a way to declare variables to be tightly typed (particularly arrays) so that it would throw an error if you used the == comparison instead of the === comparison!

Best regards, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial