PHP博客文章存储和显示[关闭]

Im creating a blog in php. (Not in wordpress or other cms)

To store all the articles and then display them must be done in database isnt it?

Is there another way to do it without using database for example to store them in an array or something.

Thanks very much

Yes, usually blog articles are stored in RDBMS aka database. If you do not want to use db, you could store them in files on disk. For example you could save each article in separate file under specific directory. When you want to display the contents of an article you could read it with php. If you want to delete an article - delete the file, etc...

Example:

Write an article to a file:

$file = 'a01.html';
$fh = fopen($file, 'w') or die("can't open file");
$contents = "Once upon a time...
";
fwrite($fh, $contents);
fclose($fh);

Delete an article:

unlink('a01.html');

Yes, as well as the creation of files, you can also use the xml. Clearly, a database will be more powerful than these solutions. Using php you can lean on tools such as DOMDocument or SimpleXML.

SimpleXML is probably the easiest to use and respect the html, xml allows you to save in an orderly manner also additional information.

Here link to XML Xml Know How

I have created a super-simple and lightweight CMS in PHP using a set of text files as the data store and generating an HTML system for the front end. It works absolutely fine as long as you can set the permissions correctly and you can be confident that you will never have to worry about SQL Injection type attacks.

However, unless you have an enormously good reason for this, I would strongly recommend against developing your own blogging system rather than using one of the hundreds of existing ones available for PHP. I have been writing code for a long time and I understand the desire to reinvent wheels as much as anyone, but honestly you could spend your time far more productively by customising existing technology to do what you need.