Ryan K
Ryan K

Reputation: 346

Would it be better to use mysql to store comments or to write them to a file or XML file in PHP and show them on the main page that way?

i am making a comment system, the user will log in with their details on the main page which has been built, but on the second page where the comments will be i want to show each comment in order of time created, would this be better done in mysql database to store the comments or by putting the comments into a file and reading them from the file's on server?

Upvotes: 0

Views: 264

Answers (5)

Sammitch
Sammitch

Reputation: 32232

XML [aka a 'flat file' database] may seem preferable for simplicity's sake, but you will find that your performance degrades ridiculously fast once you get a reasonable amount of traffic. Let's say that you have a separate XML file storing comments for each page, and you want to display the last 5 comments with the newest first. The server has to read the entire file from start to finish just figure out which 5 comments are the last.

What happens when you have 10,000 comments on something? What happens when you have 100 posts with 10,000 comments and a 1000 pageviews per second? Basically you're putting so much I/O load on your disk that everything else will grind to a halt for queued I/O.

The point of an RDBMS like mysql is that the information is indexed when it is put into the database, and that index is held in memory. In this way the application doesn't have to re-examine 100% of the data each time a request is made. A mysql query is written to consult the index and have the system retrieve only the desired information, ie: the last 5 comments.

Trust me, I worked for a hosting provider and code like this is constantly causing problems of the variety "it worked fine for months and now my site is slow as hell". You will not regret taking a little extra time to do it right the first time rather than scrambling to re-do the application when it grinds to a halt.

Upvotes: 3

nickhar
nickhar

Reputation: 20823

MySQL every time!

It's perfect for doing this and very suitable/flexible/powerful.

Upvotes: 0

JDavies
JDavies

Reputation: 2770

I would recommend a database, a lot easier to access the data that way. XML is always a pain when it comes to laying out the code when pulling out the data. Might be worth getting a more detailed explanation from someone though as i've not had that much experience with XML.

Plus you have more control over moderating the comments.

Hope that helps!

Upvotes: 0

Dhairya Vora
Dhairya Vora

Reputation: 1281

Storing the comments in database will be a better option. You get more power with database. By word more power, I mean you can easily do these:

  • When use gets deleted, you can decide if you want to delete his comments OR not
  • You can show top 10(or so) comments
  • You can show all comments from a user on some other page
  • Etc

Upvotes: 0

Sablefoste
Sablefoste

Reputation: 4182

Definitely in MySQL. The actions are easy, and traceability is easy.

Unless you want to go the NoSQL route.

Upvotes: 0

Related Questions