$value) { if (in_array($key, $rfields)) { $dataTypes[] = $key; } } // Did we get any valid parameters? if (count($dataTypes) == 0) { die('No valid return data types supplied'); } // Connect to the datbase $mysqli = new mysqli($dbhost, $dbuser, $dbpassword,$database); if ($mysqli->connect_errno) { die('Failed to connect to the database server'); } if ($mysqli->connect_errno) { die('Failed to connect to the database on the server'); } $result = $mysqli->query('SET time_zone="+0:00"'); if (!$result) { die('ERROR - TZ Statement'); } // Was the 'nth' parameter supplied? If not, do a standard query, else, only return every nth row from the table if ($nth === '') { // Start of query string, then... $query = 'SELECT UNIX_TIMESTAMP(LogDateTime)'; //$query = 'SELECT LogDateTime'; // add query data columns, then.. foreach($dataTypes as $key) { $query .= ', ' . $key; } // end of query string. //$query .= " FROM $logtable WHERE LogDateTime > DATE_SUB($strtd, INTERVAL $recordAge $recordUnit) and LogDateTime < DATE_ADD($strtd, INTERVAL 1 day) ORDER BY LogDateTime ASC"; if ($recordAge === 'all') { $query .= " FROM $logtable ORDER BY LogDateTime ASC"; } else { $query .= " FROM $logtable WHERE LogDateTime > DATE_SUB($strtd, INTERVAL $recordAge $recordUnit) and LogDateTime < DATE_ADD($strtd, INTERVAL 1 day) ORDER BY LogDateTime ASC"; } } else { // Only return every nth row // Start of query string, then... $query = 'SELECT ts'; // add query data columns, then.. foreach($dataTypes as $key) { $query .= ', ' . $key; } $query .= ' FROM ( SELECT @row := @row +1 AS rownum, UNIX_TIMESTAMP(LogDateTime) AS ts'; // add query data columns, then.. foreach($dataTypes as $key) { $query .= ', ' . $key; } // end of query string. $query .= " FROM $logtable WHERE LogDateTime > DATE_SUB($strtd, INTERVAL $recordAge $recordUnit) ORDER BY LogDateTime ASC) as DT WHERE MOD(rownum,$nth)=0"; // initialise the row counter $result = $mysqli->query('SET @row := 0;'); if (!$result) { die("ERROR 1- Bad SQL Statement: $query"); } } // call when was mysql : http://meteo.laurentmey.fr/sqli/monthLogSql.php?recordAge=12&recordUnit=day&strt=%222020-03-01%22&Temp // $query .= " FROM $logtable WHERE LogDateTime > DATE_SUB($strtd, INTERVAL $recordAge $recordUnit) ORDER BY LogDateTime ASC) as DT WHERE MOD(rownum,$nth)=0"; // call for test : http://meteo.laurentmey.fr/sqli/monthLogSqli.php?recordAge=12&recordUnit=day&Temp //$query = "SELECT UNIX_TIMESTAMP(LogDateTime), Temp, Humidity FROM AllCdata WHERE LogDateTime > (SELECT MAX(LogDateTime) - INTERVAL 12 day FROM AllCdata) ORDER BY LogDateTime ASC"; //$query .= " FROM $logtable WHERE LogDateTime > DATE_SUB($strtd, INTERVAL $recordAge $recordUnit) ORDER BY LogDateTime ASC) as DT WHERE MOD(rownum,$nth)=0"; // Perform query $result = $mysqli->query($query); if (!$result) { die("ERROR 2- Bad Select Statement: $query"); } // for each of the returned rows, put the data into a series of arrays... while($row = $result->fetch_array()) { $tim = $row[0] * 1000; $i = 1; foreach($dataTypes as $key) { ${"arr_$key"}[] = array($tim, (float)$row[$i++]); } } // build the return array foreach($dataTypes as $key) { $return[$key] = ${'arr_' . $key}; } header('Cache-Control: private'); header('Cache-Control: no-cache, must-revalidate'); header('Content-type: text/json; charset=UTF-8'); // encode the result and echo it back to the client echo json_encode($return); // close connection $mysqli->close(); // ========== End of Module =========== ?>