dag_karlsson

Untitled

Apr 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. $servername = '127.0.0.1';
  2. $username = $GLOBALS['username'];
  3. $password = $GLOBALS['password'];
  4. $dbname = 'ithaax_ASPire_config';
  5. $db = new mysqli($servername, $username, $password, $dbname);
  6.  
  7. $table = "dataLogs_marine_sensors";
  8. $sql= "SELECT MAX(id) FROM $table";
  9. $result = $db->query($sql);
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. $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"}]}
  21. ';
  22.  
  23. $data = json_decode($data,true);
  24.  
  25. if (!empty($data))
  26. {
  27.  
  28. foreach ($data as $table_name => $table)
  29. {
  30. print_r($table_name);
  31. // Generate the array to be bind with the prepared SQL query
  32. foreach ($table as $id_log => $log)
  33. {
  34. $param_stmt = "(";
  35. $param_to_fill = "(";
  36. $param_array = array();
  37. foreach ($log as $column_name => $value)
  38. {
  39. $param_array[$column_name] = $value;
  40. $param_stmt = $param_stmt . ''.$column_name .',' ;
  41. $param_to_fill = $param_to_fill . ':'.$column_name.',';
  42. }
  43.  
  44. // Remove the extra comma
  45. $param_stmt = substr($param_stmt, 0, -1).')';
  46. $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
  47.  
  48. $param_array['id'] = NULL;
  49. // Prepare the SQL Query
  50. $query = $db->prepare("INSERT INTO $table_name $param_stmt VALUES $param_to_fill ;");
  51. $query->execute($param_array);
  52.  
  53.  
  54. $sql= "SELECT MAX(id) FROM $table_name";
  55. $result = $db->query($sql);
  56. $idMap[$table_name] = $result->fetch_assoc()['MAX(id)'];
  57.  
  58. }
  59.  
  60. }
  61. //$result = array('result' => 1);
  62. //return json_encode($result);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment