I have some data stored in key values in a list on my website (say a simple HTML select). The data can be matched with key value pairs as numbers or strings in the database like :
Php array (
1=>high school
2=>university
Etc
Then stored in a database.
Or
High school => High school
University => university
Etc
Now this is repeated throughout my site.
The question is if I store the key as numbers in the database, php will have to match back with some form of switch statement.
So which is the best way to optimize my site - have numbers in the database (which makes for I understand a faster database) and have php match back to values?
Or just store the correct string in the database in the first place (which I understand is slower for database)?
The keys are going to be reasonably static in this instance. But this question keeps popping up as I go through my site.
I guess the question comes down to which tool is best and is a helpful for understanding.
You should be storing the key in the db. It is not clear what the actual problem is you are asking about.
If you are trying to populate a dropdown from the DB you would just select the id and description field and then foreach over the results to populate the dropdown.
SELECT colum_id, description FROM lookup_table
If you are trying to display the stored result you would us a JOIN to, well, join the description to the id.
Sorry if I was unclear : my question do I store the key to the select HTML (not the primary key) as an integer or just directly as a string in the database ?
Which makes for the fastest website?
I donât understand why you would store key/value pairs in an array and the database.
One makes the other redundant.
You would use one or the other, not both.
I understand that integers are faster in databases than strings
I have some HTML select fields. The HTML can be option 1:
HTML
<select name=âeducationâ>
<option value=1>High school</option>
<option value=2>University</option>
</select>
PHP $education = $_Post[âeducation]
If $education = 1 print âhigh schoolâ else (switch) etc if 2 print âŚ
MySQL
This education value is stored in a MySQL database column with he title education. Here the column type is Int and high school is selected the value is 1.
MySQL:
education int = 1.
I then insert the key into the database as 1 or 2 and then use a php if type tool to print the value to screen later. If 1 print âhigh schoolâ if education = 2 print university.
If I were going to take the path of using a database, which I may if the data were likely to change and be updated with any degree of frequency, I would do as suggested, have a PK as an integer and a text string value. These could be fetched as key pairs via PDO::FETCH_KEY_PAIR.
If the data was unlikely to change and not too many rows, it could be hard coded into the PHP, I may use an array of key/value pairs.
In that case, definitely use a database and store the value as an integer.
The text string would be in another table with its PK (ID) matching the integer you store with the student ID.
So you have one table âeducationâ with columns id and place with values like 1 and High School
Then a table matching students to education with a student_id and education_id as integers.
It may be that you just add the education (int) column to your student table, if they will only ever have one value for that.
Thanks for your endless patience â this is why I use this forum. Others when an idea is explained by beginners it is often just downvoted or gets a snarky reply :(!