12

I am creating a custom CMS in PHP (written from scratch) and want to know whether I should store user created pages as files or in a MySQL database.

The content is all HTML code, at least for now.

I cannot decide which to do as writing files with php seems like a security risk, and retrieving file contents from MySQL on every page load feels wrong (and could be a performance issue?).

I also have custom pages coded by me for a blog, etc. These contain PHP code but do not need to be modified by the user. Currently I am planning to store these as php files as its easier to upload them and edit them like that, but they could also be stored in MySQL.

I would greatly appreciate any help on what the right thing to do is, or at least what you would do.

bakkal
  • 54,350
  • 12
  • 131
  • 107
Nico Burns
  • 16,639
  • 10
  • 40
  • 54
  • 1
    MySQL is plenty fast. CMSs typically don't store whole pages in the database, though. They store page *content*, meaning things like templates and CSS are still stored as files. PHP stitches the different parts together when a user wants to see a page. You'll have to be pushing hundreds of thousands or millions of views per day through MySQL before you see performance issues (provided you have decent design and basic optimization). – cbednarski Aug 02 '10 at 01:32

1 Answers1

13

You'd be better off storing content in the database. Note that you store content, not the entire HTML page (otherwise, you're not really building a content management system).

If you implemented it using plain files then you'd have to invent a file format for storing your structured data in, you'd have to figure out how to make it fast, worry about data integrity and race conditions, and more. With a database you get all this already done for you and it's done well and fast.

It's quite normal to do a database query on every page view; indeed it is typical for web apps to do more than 5 and up to 30 database queries on every page view, and I would guess that stackoverflow.com would probably fit into that range.

MySQL is fast enough.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167