Reputation: 39
I've a PHP with far too many query (like 20) and each one has a few echo with var. I would like to generate a JSON with those vars.. in a single JSON File.
Example:
// QUERY 1
$query = "SELECT * FROM $table1 WHERE DateV = '$dateV' AND StatusV = 'WORKING'";
$result = mysql_query($query) or die("Error : $query. " . mysql_error());
$row = mysql_fetch_array($result);
echo '&Tvar=' . $row['num'];
echo '&Tvarname=' . $row['name'];
// QUERY 2
$query = "SELECT * FROM $table2 WHERE DateV = '$dateV' AND StatusV = 'WORKINGFAST'";
$result = mysql_query($query) or die("Error : $query. " . mysql_error());
$row = mysql_fetch_array($result);
echo '&Bvar=' . $row['something'];
echo '&Bvarname=' . $row['something'];
and since I need to load that data, from an Desktop AIR App, every 2 seconds from multiples intances I don't wanna overload the MYSQL Server. Anyway, I believe the best thing I can do, it's to generate a single file and load that one as many times as I need.
Thank you!
Upvotes: 0
Views: 80
Reputation: 2792
You can add your values to an array and use json_encode(). If you're worrying about overloading your MySQL server, I can say that this query every 2 seconds is not a problem.
It's a good practice to cache results which doesn't change every few calls. So you can take a look at memcached or just use a simple file cache with file_put_contents() and file_get_contents().
You can try it with a code similar to this below:
$jsonFile = "my.json";// path to your json file
$timeToCache = 1 * 60;// cache time in seconds
// see if the file exists and if the file is older than the time we want to cache
if( file_exists( $jsonFile ) && ( ( time() - filemtime( $jsonFile ) ) > $timeToCache ) ) {
$toJson = array();
// your first query goes here
$row = mysql_fetch_array($result);
$toJson["Tvar"] = $row['num'];
$toJson["Tvarname"] = $row['name'];
// your second query goes here
$row = mysql_fetch_array($result);
$toJson["Bvar"] = $row['something'];
$toJson["Bvarname"] = $row['something'];
// put your json output to some file
$jsonOut = json_encode( $toJson );
file_put_contents( $jsonFile , $jsonOut );
} else {
$jsonOut = file_get_contents( $jsonFile );
}
echo $jsonOut;
And you should also stop using mysql_* functions, because they are deprecated:
Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used
Take a look at Why should I use prepared statements.
Upvotes: 1
Reputation: 1124
Try this
// QUERY 1
$query = "SELECT * FROM $table1 WHERE DateV = '$dateV' AND StatusV = 'WORKING'";
$arrfinal = array();
$result = mysql_query($query) or die("Error : $query. " . mysql_error());
$row = mysql_fetch_array($result);
$arrfinal['first'] = array('Tvar'=>$row['num'], 'Tvarname'=>$row['name']);
// QUERY 2
$query = "SELECT * FROM $table2 WHERE DateV = '$dateV' AND StatusV = 'WORKINGFAST'";
$result = mysql_query($query) or die("Error : $query. " . mysql_error());
$row = mysql_fetch_array($result);
$arr = array(
'Bvar' =>$row['something'],
'Bvarname' => $row['something']
);
$arrfinal['second'] = $arr;
echo json_encode($arrfinal);
Upvotes: 1