-1

I have a fixed (rarely changing) list of ~100 words. I want to display a random word on my HTML page on every reload. Should I hardcode the words as an array in the PHP script, or should I put them into a MySQL table and pull a random entry from there? What are the possible performance/maintainability considerations here?

jamix
  • 5,484
  • 5
  • 26
  • 35
  • In your case, hardcoded array is a no-brainer. You don't need MySQL for that whatsoever. – N.B. Jul 22 '14 at 13:58
  • If you're already using MySQL for other aspects of the site, I'd put it in a table. But don't go to the trouble of setting up MySQL just for that. – Ivan Jul 22 '14 at 14:00
  • You could also use [SQLite](http://php.net/manual/en/book.sqlite.php) in case you wanna use some kind of database thing. – Olli Jul 22 '14 at 14:30

5 Answers5

1

It depends, if you ever want to easily manage these words or have someone else manage them, I would go for putting them into a database. Using a database has an extremely high overhead relative to a PHP array, although it is likely unnoticeable for a human if hosted locally.

I would not use anything other then a PHP array, database table, or text file though. I think that even a text file is a little bit extraneous and shouldn't be used - if you would want it in a text file it's probably just best to put it in a database.

Hans
  • 233
  • 1
  • 9
1

My take is that it depends on these factors:

  1. How rarely is "rarely"? Like once every year? Or maybe once every month?
  2. How many requests are you getting, and how many are you predicting?
  3. Do you have an established development/deployment cycle? Meaning, steps between changing some code and actually updating in production servers.
  4. Do you have direct access to the databse? Or would you have to set up and admin tool in order to edit the list?

I would favor a non-MySQL scenario if you don't use the DB for anything else, or if you are getting millions of requests per day, so as not to add millions of queries for such a simple objective. Maybe using a local file with the words would suffice, if you have relatively straightforward access to the filesystem.

Mariano D'Ascanio
  • 1,202
  • 2
  • 16
  • 17
0

I wouldn't go for MySQL as its just not needed for a non-changing set of words. If you plan to change them whatsoever go for a CSV file [using implode() and explode() to manage it] or if you are very rarely or never changing them then a PHP Array would be best for performance, with 0 maintenance.

t3chguy
  • 1,018
  • 7
  • 17
  • For one column csv files, it is ok. But it wouldn't be too good if some people think that parsing multi column csv files with explode is good idea. So just for those people, see for example this question: http://stackoverflow.com/questions/8073047/how-do-you-explode-csv-line-with-a-comma-in-value – David Kudera Jul 22 '14 at 14:04
  • Yeah probably should mention that, a CSV file wouldn't even be required as `implode()` and `explode()` aren't ways of managing a CSV file. – t3chguy Jul 22 '14 at 14:04
0

If you wanted to change the words via a nice interface you were going to write, I'd store them in MySQL. If they rarely change and it's just as easy to update your code as it is the database, then you might as well just store them in a PHP array.

MattyB
  • 909
  • 2
  • 9
  • 15
0

I will say to use hardcode as php array instead of mysql and other connectivity part for these so it will be easy and you are saying you will use it rarely.

Parimal
  • 183
  • 1
  • 11