I'm currently working on a project where i have to extract some data from an online XML web service about earthquake and insert the data into a database.
My connection seems to be working since it displays "connected successfully" in my localhost web page.
The part where the code is extracting the data location, date & time, latitude longitude and magnitude does not seem to be working as it should have been displaying the success or failure message. But nothing is being displayed for this part. The data are not being in the table in phpMyAdmin.
I added an echo to see if the file is being read till the end and it showed the message "file read complete".
Here is my code:
<?php $url="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.quakeml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
$db_user = 'root';
$db_pass = '';
$db_name = 'Earthquake';
$db_host = 'localhost';
$con = mysqli_connect("$db_host", "$db_user", "$db_pass", "$db_name");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Connected successfully";
}
//GETTING LOCATION OF EARTHQUAKE
foreach ($xml -> description as $row ){
$location = $row -> text;
$sql = "INSERT INTO tbl_earthquake (Location) VALUES (location)";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
} }
// GETTING DATE AND TIME OF EARTHQUAKE
foreach ($xml -> time as $row){
$datetime = $row -> value;
$sql = "INSERT INTO tbl_earthquake (DateandTime) VALUES ($datetime)";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
}
//GETTING LATITUDE OF EARTHQUAKE
foreach ($xml -> latitude as $row){
$latitude = $row -> value;
$sql = "INSERT INTO tbl_earthquake (Latitude) VALUES ($latitude)";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
}
//GETTING LONGITUDE OF EARTHQUAKE
foreach ($xml -> longitude as $row){
$longitude = $row -> value;
$sql = "INSERT INTO tbl_earthquake (Longitude) VALUES ($longitude)";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
}
//GETTING MAGNITUDE OF EARTHQUAKE
foreach ($xml -> mag as $row){
$magnitude = $row -> value;
$sql = "INSERT INTO tbl_earthquake (Magnitude) VALUES ($magnitude)";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
var_dump($xml->description, $xml->time, $xml->latitude, $xml->longitude, $xml->mag);
echo"file read complete;"
?>