PHP MySQL Working with Barcodes

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I'm simply trying to store a barcode in a mysql db and the trouble I'm having is handling the zeros.

Say i'm storing

012345678912

but only 123456789 gets put in the db. So, I changed the barcode column to zerofill and it fixes it. However, the column is set to 12 digits cause I may have some that are 9 digits long, 10 digits long, etc. Therein lies my problem. I assume a barcode that is 9 digits long and put into a 12 int field that is zerofill will become 000xxxxxxxx9 where the three leading zeros will fill out the field. Making comparisons to the actual scanned value difficult.

I just want to store the barcode exactly like it's gonna scan. Maybe I need to change the field to varchar instead of int, but didn't know what the consequences may be down the road writing search queries or if I will still have the same problem of dropping/adding zeros with a zerofill field. Please let me know if there's any other details/explanation needed to help get me on the right path. Many thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I'd use the VARCHAR.

I can't think of any situations where a string wouldn't be usable. And if there are any you can always use type casting to get the correct result.

There's always the option of setting up a VARCHAR and INT field, and using a trigger to update the INT field whenever the VARCHAR changes.

An index could be created for the number of numbers in the field.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Gotcha. Thanks for the reply. Just wanted to make sure I wasn't going to run into issues down the road using varchar and if I do, I now know some options to fix it. Thanks for the extra details.

Thanks again.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 109 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.