I'm designing a little web-app/game. What would be better: MySQL tables or json files? They both store information. They can both be parsed by PHP. What are the advantages/disadvantages?
This is what I mean:
username | password ------------------- seefour | abc123
vs.
{ "username":"seefour", "password":"abc123" }
EDIT: Wow, It's been just 3 years since I asked this question and it's surprising to see how much I've matured since when I asked this question. From a future me to the past me, this is why the two don't work. (In case anybody naive like me at the time can refer to this)
I used to think the two were interchangeable because they were both pretty much ways of storing information, although storing and using JSON files was easier to me at the time. Databases are separate pieces of software that make retrieving data much faster and don't end up being bloated over time. Also, carrying all the data in one or two files makes it dangerously easy to end up getting your data stolen or lost, where as a database is much more secure with those. Fundamentally, data shouldn't be part of your code; it should be a separate thing that your code works with.
Also, you'll learn about hashing and salting a couple of years down the line, so don't store passwords in plain text!
The drawback? If your JSON has multiple fields with the same key, only one of them, the last one, will be retained. The other drawback is that MySQL doesn't support indexing JSON columns, which means that searching through your JSON documents could result in a full table scan.
MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.
From the tests I did Inserting or storing data MySQL was quicker and the code is cleaner even though you still have to design the database layout and schema it's still worth it to use MySQL. Reading and fetching data JSON was quicker, i was surprised!
A relational database makes sense for fast and efficient storage and retrieval of data that has relational properties. JSON is a great data format because it is simple, lightweight and ideal for passing around raw data in a very basic format with a syntax suited to storing and exchanging text information.
To be really blunt about, MySQL is a database while JSON is not, so the correct answer is MySQL, without hesitation. JSON is just a language, and barely even that. JSON was never designed to handle anything like concurrent connections or any sort of data manipulation, since its own function is to represent data, not to manage it.
So go with MySQL for storing the data. Then you should use some programming language to read that database, and send that information as JSON, rather than actually storing anything in JSON.
If you store the data in files, whether in JSON format or anything else, you will have all sorts of problems that people have stopped worrying about since databases started being used for the same thing. Size limitations, locks, name it. It's good enough when you have one user, but the moment you add more of them, you'll start solving so many problems that you would probably end up by writing an entire database engine just to handle the files for you, while all along you could have simply used an actual 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