I am new to PHP but not programming in general. I want to store some data I retrieve from the web service but I do not think I want a database for that. First, data will be updated quite frequently and its size is always less than 1MB. What is the best, fast but efficient approach in PHP on Apache? Note: I am using a hosting provider so I do not prefer custom installations. Perhaps, singleton? Thanks
-
1Why don't you want to use a database? That sounds perfect for what you are doing. If you want the data to last indefinitely, the only other option would be to store it to a file, which is practically the same thing except harder to manage. A "singleton" is just a design pattern (really a buzzword now) that has nothing to do with data storage and no specific meaning.Explosion Pills– Explosion Pills2011-02-25 17:36:54 +00:00Commented Feb 25, 2011 at 17:36
-
Your question is too vague. Describe the data usage. Otherwise you'll have to live with the generalized "just use a database" answers.mario– mario2011-02-25 17:53:26 +00:00Commented Feb 25, 2011 at 17:53
7 Answers
I think the question becomes, how long will the data be stored for? If you are storing the data until it is replaced or longer than a single user session, personally I believe a database is the ideal solution - it is designed to be very efficient with quickly altering and retrieving data.
If the data is only kept for a single user session, you could use PHP sessions to store the data as an array.
The other alternative is to store the data in a file. However, this can be much less efficient in retrieving small amounts of data.
Comments
The obvious alternative to the database is file storage. PHP can read and write good old disk files; see fopen(), fread(), fwrite() and then some. You'll need to designate a folder on the server (aside from the public_html space) for file(s) and come up with a naming scheme and data format.
Comments
You can store your data in xml files and use an simplexml to load the data an manage it, like:
$xml = simplexml_load_file("test.xml");
Then you can have a list of the nodes you defined and do your stuff. For further reference you can check out the following:
Comments
You could save arrays in a plain text file, with serialize.
Something like
$fh = fopen("db.txt", "w");
fwrite($fh, serialize(array("field"=>"data"));
fclose($fh);
Retrieve it again with fread and mode "r" and then the method unserialize.
$fh = fopen("db.txt", "r");
$data = unserialize(fread($fh));
fclose($fh);
And then manage your data in the array.