Unanswered: need help with MySQL vs. arrays dilemma
I got the following question. I use countries in my site - there are almost 250 of them.
Which variant will be faster for extracting country name by its code and creating a <select> with those countries? And which variant is less resource-consuming?
1. Create an array, say, $countries with keys as country codes (i.e. "eg" for "Egypt").
2. To extract country name by stored code use $country[$country_code]
3. To make a <SELECT use a function with WHILE ($element=each($country)) cycle, putting out all <OPTIONs as country code/country name pairs
1. Create a MySQL table with country codes as unique fields
2. To extract country name access a db and pull out a name with SELECT ... WHERE country_code='eg'
3. To make a <SELECT use a function with SELECT * FROM that_table_with_countries...
Any ideas? Any suggestion would be greately appreciated!
Well, undoubtedly, using an in-memory array will be much faster than retrieving data from your database (though it would not take long). However, that means that you would have the list of countries/country codes hard-coded instead of having them in the database and reload them dynamically.
In the case of countries, the list won't change tomorrow , so I think you can afford to hard-code it. Now, if you already have a MySQL database, creating a table and then execute queries on it won't be too heavy in this case. The second solution is cleaner and I may prefer this one. However, you will lose in terms of performance : in-memory work is always faster. Moreover, though in a non-web environment the difference may be unnoticeable with such a query, remember that if you use a table in a web environment, you will have to reload the page when you execute the query so as to reach the MySQL server, while you wouldn't have to if the list was hard-coded.