I am newer for php. I want make php page cache, query data from mysql and store data into json format.
I have many questions:
-
which type of file should I store?
.jsonor.txtor.cache? for I also need use json decode return datas into page. -
I want use cron tab, make many mysql queries and write into one json file. what write code should I choose?
fopen, fwriteorfile_get_contentsor other command? (do not cover the data, but continue write. I will deleted the file and renewer it at the next cron time) -
If a multi write into a json data (10 or more mysql query at the same time and write into a same json file, each json child format like
{name: ".$row['name']."}), how to completed a top{and bottom}to make a standad json data format?{ //how to add this one
{name: “.$row[‘name’].”}
{name: “.$row[‘name’].”}
// many name from 10 more mysql queries
} //and this one
Thanks.
It doesn’t matter. There is no fixed extension, but I would pick
.jsonjust to make it clear what the file is supposed to contain.Just use file_put_contents to put the JSON string (see next section) into a file.
You really do not want to use that method. It might work for a while, but becomes very complex when you need to handle things like quoting and special-character escapes. Instead of re-inventing the wheel, use PHP’s built-in JSON functions for this.
Create the data-structure you want using PHP’s strings, numbers, and arrays, and then rely on
json_encodeto turn it into a string.The main thing to be careful of is that depending on how your php
array()looks, you might get JSON[]versus{}.