Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $servername = '127.0.0.1';
- $username = $GLOBALS['username'];
- $password = $GLOBALS['password'];
- $dbname = 'ithaax_ASPire_config';
- $db = new mysqli($servername, $username, $password, $dbname);
- $table = "dataLogs_marine_sensors";
- $sql= "SELECT MAX(id) FROM $table";
- $result = $db->query($sql);
- $data='{"dataLogs_actuator_feedback":[{"id":"115","rc_on":"0","rudder_position":"-2000.0","t_timestamp":"2018-04-24 12:25:06.513","wind_vane_angle":"-2000.0","wingsail_position":"-2000.0"}],"dataLogs_compass":[{"heading":"32.0","id":"115","pitch":"0.0","roll":"0.0","t_timestamp":"2018-04-24 12:25:06.513"}],"dataLogs_course_calculation":[{"bearing_to_waypoint":"181.0","course_to_steer":"182.2476501","distance_to_waypoint":"171.1798706","going_starboard":"1","id":"115","t_timestamp":"2018-04-24 12:25:06.513","tack":"0"}],"dataLogs_current_sensors":[{"actuator_unit":"-2000.0","id":"115","navigation_unit":"-2000.0","sailboat_drive":"-2000.0","t_timestamp":"2018-04-24 12:25:06.513","wind_vane_angle":"-2000.0","wind_vane_clutch":"-2000.0"}],"dataLogs_gps":[{"course":"218.0","has_fix":"1","id":"115","latitude":"60.10723877","longitude":"19.92240334","online":"1","route_started":"0","satellites_used":"0","speed":"0.05841402337","t_timestamp":"2018-04-24 12:25:06.513","time":"2018-04-24 12:25:06.513"}],"dataLogs_marine_sensors":[{"conductivity":"1.000200033","id":"115","ph":"7.000199795","salinity":"0.009951953776","t_timestamp":"2018-04-24 12:25:06.513","temperature":"20.00020027"}],"dataLogs_system":[{"actuator_feedback_id":"115","compass_id":"115","course_calculation_id":"115","current_mission_id":"1501671669","current_sensors_id":"115","gps_id":"115","id":"115","marine_sensors_id":"115","vessel_state_id":"115","wind_state_id":"115","windsensor_id":"115"}],"dataLogs_vessel_state":[{"course":"46.0","heading":"46.0","id":"115","latitude":"60.10723877","longitude":"19.92240334","speed":"-0.06806444377","t_timestamp":"2018-04-24 12:25:06.513"}],"dataLogs_wind_state":[{"apparent_wind_direction":"225.0","apparent_wind_speed":"3.046408176","id":"115","t_timestamp":"2018-04-24 12:25:06.513","true_wind_direction":"271.9196472","true_wind_speed":"2.998665571"}],"dataLogs_windsensor":[{"direction":"230.0","id":"115","speed":"3.03659749","t_timestamp":"2018-04-24 12:25:06.513","temperature":"21.0"}]}
- ';
- $data = json_decode($data,true);
- if (!empty($data))
- {
- foreach ($data as $table_name => $table)
- {
- print_r($table_name);
- // Generate the array to be bind with the prepared SQL query
- foreach ($table as $id_log => $log)
- {
- $param_stmt = "(";
- $param_to_fill = "(";
- $param_array = array();
- foreach ($log as $column_name => $value)
- {
- $param_array[$column_name] = $value;
- $param_stmt = $param_stmt . ''.$column_name .',' ;
- $param_to_fill = $param_to_fill . ':'.$column_name.',';
- }
- // Remove the extra comma
- $param_stmt = substr($param_stmt, 0, -1).')';
- $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
- $param_array['id'] = NULL;
- // Prepare the SQL Query
- $query = $db->prepare("INSERT INTO $table_name $param_stmt VALUES $param_to_fill ;");
- $query->execute($param_array);
- $sql= "SELECT MAX(id) FROM $table_name";
- $result = $db->query($sql);
- $idMap[$table_name] = $result->fetch_assoc()['MAX(id)'];
- }
- }
- //$result = array('result' => 1);
- //return json_encode($result);
- }
Advertisement
Add Comment
Please, Sign In to add comment