2

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,

Yosef Naser
  • 45
  • 2
  • 8

5 Answers5

2

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;
Stuart M
  • 11,458
  • 6
  • 45
  • 59
1

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.

Somy A
  • 1,682
  • 15
  • 18
0

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.

d'alar'cop
  • 2,357
  • 1
  • 14
  • 18
0
  1. I suppose that's just incorrect value (for the int field type) and cannot be stored at all.
  2. Database intended to store data, means data without any additional formatting, which can be added at select time. That's how this thing works in general.

So, I'd suggest to store a regular int, and pad it with whatever number of zeros you wish at select time

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Try to use query in mysql.

ALTER TABLE table MODIFY COLUMN id INT(3) ZEROFILL;

this may solve your question.

Jason.Jan
  • 61
  • 1
  • 8