I am trying to add the value "006" to an MySql row, but it shows up as "6"! any fixes on my code ?
$id = "006";
mysql_query("INSERT INTO Table (id) VALUES ($id) ");
Thanks,
I am trying to add the value "006" to an MySql row, but it shows up as "6"! any fixes on my code ?
$id = "006";
mysql_query("INSERT INTO Table (id) VALUES ($id) ");
Thanks,
If id
is a numeric column such as an integer, 006
is the same as 6
so there isn't a way to explicitly save 006
since its exactly the same as 6
.
But if you want to retrieve that integer as a 3-digit, zero-padded string, you could use the LPAD
function to pad it with 0
's on the left side:
SELECT LPAD(id, 3, '0') as id FROM table;
Although this is not a safe way:
$id = "006";
mysql_query("INSERT INTO Table (id) VALUES ('$id') ");
I think you forgot some quotes on values, so its cast to int.
Your field needs to be a VARCHAR for that to work as you desire. You may also pad leading zeroes when you run your queries as an alternative.
So, I'd suggest to store a regular int, and pad it with whatever number of zeros you wish at select time
Try to use query in mysql.
ALTER TABLE table MODIFY COLUMN id INT(3) ZEROFILL;
this may solve your question.