Estoy intentando insertar valores de un Json a una tabla de mi base de datos CON PHP, esto es lo que tengo:
$json_pagina = file_get_contents('https://pagina');
$datos_json = var_dump(json_decode($json_pagina));
$server = "localhost";
$user = "root";
$pass = "";
$bd = "datqa";
$conexion = mysqli_connect($server, $user, $pass,$bd);
if (!$conexion) {
die("Falló la conexión: " . mysqli_connect_error());
}
echo "Conectado exitosamente";
foreach($datos_json as $json_data){
mysqli_query($conexion,"INSERT INTO json_data (suggested_pickup,suggested_pickup_period,dewpoint_c,dewpoint_f,dewpoint_string,heat_index_c,heat_index_f,heat_index_string,location,latitude,longitude,observation_time ,observation_time_rfc822,pressure_in,pressure_mb,pressure_string,relative_humidity,station_id ,temp_c,temp_f,temperature_string,wind_degrees,wind_dir,wind_kt ,wind_mph ,windchill_c,windchill_f,windchill_string)
VALUES (".$json_data['suggested_pickup']."','".$json_data['suggested_pickup_period']."','".$json_data['dewpoint_c']."','".$json_data['dewpoint_f']."','".$json_data['dewpoint_string']."','".$json_data['heat_index_c']."','".$json_data['heat_index_f']."','".$json_data['heat_index_string']."','".$json_data['location']."','".$json_data['latitude']."','".$json_data['longitude']."','".$json_data['observation_time']."','".$json_data['observation_time_rfc822']."','".$json_data['pressure_in']."','".$json_data['pressure_mb']."','".$json_data['pressure_string']."','".$json_data['relative_humidity']."','".$json_data['station_id']."','".$json_data['temp_c']."','".$json_data['temp_f']."','".$json_data['temperature_string']."','".$json_data['wind_degrees']."','".$json_data['wind_dir']."','".$json_data['wind_kt']."','".$json_data['wind_mph']."','".$json_data['windchill_c']."','".$json_data['windchill_f']."','".$json_data['windchill_string'].")");
}
mysqli_close($conexion);
var_dump
a la variable$datos_json
var_dump
? asi no podemos entender si el bucle foreach tiene datos correctos para iterar. Inicialmente se me ocurre que deberias ponerjson_decode($json_pagina, true)
para devolver un array asociativo en lugar de objetos