How should I store States and Countries?
Option 1) I could create a database table called states, and have 'id', 'state', and 'state_abbr'. I could then reference that by 'id' in the users table.
Option 2) I could create a PHP array and use that to populate the states drop down menu, and avoid querying the database altogether. The key for the array would be the value inserted in the database for that user.
States don't change, and so the data should be static. What's the best way to store this information and why? Wouldn't a PHP array be faster? Why does it seem like the database is the best way to store it then?
Thoughts?
A wise database designer once told me, "Everything you know about state codes is wrong."
Given that you're not likely to know all these things about such a common thing as USPS state codes, it just makes more sense to store them in a table. Tables are easier to maintain at run time than a PHP array is.
Because the database is the best way to store it.
What if next year your company decides to go international? So now you need countries, and a dependent dropdown for states.
So you need to add europe, then africa.. Thats a TON of countries, along with a ton of states. You will need one big array.
Then your boss wants a report by country.
Much easier to manage if you have it all in a database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With