exec("SET FOREIGN_KEY_CHECKS=0"); $pdo->exec("TRUNCATE TABLE hurricanes"); $pdo->exec("SET FOREIGN_KEY_CHECKS=1"); $file = fopen($csv_file, 'r'); // Get header rows $header1 = fgetcsv($file); $header2 = fgetcsv($file); // Find column indices $sid_index = array_search('SID', $header1); $season_index = array_search('SEASON', $header1); $name_index = array_search('NAME', $header1); $iso_time_index = array_search('ISO_TIME', $header1); $lat_index = array_search('LAT', $header1); $lon_index = array_search('LON', $header1); $wind_index = array_search('WMO_WIND', $header1); $pres_index = array_search('WMO_PRES', $header1); $basin_index = array_search('BASIN', $header1); $current_year = date('Y'); $ten_years_ago = $current_year - 10; $count = 0; $processed_sids = []; $pdo->beginTransaction(); while (($row = fgetcsv($file)) !== FALSE) { $season = (int)$row[$season_index]; $basin = $row[$basin_index]; $sid = $row[$sid_index]; if ($season >= $ten_years_ago && $basin === 'NA') { if (!in_array($sid, $processed_sids)) { if (count($processed_sids) >= 2) { break; // Stop after processing two storms } $processed_sids[] = $sid; } $stmt = $pdo->prepare( 'INSERT INTO `hurricanes` (`storm_id`, `season`, `name`, `iso_time`, `lat`, `lon`, `wind_speed`, `pressure`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' ); $stmt->execute([ $sid, $season, $row[$name_index], $row[$iso_time_index], (float)$row[$lat_index], (float)$row[$lon_index], (int)$row[$wind_index], (int)$row[$pres_index] ]); $count++; if ($count % 1000 === 0) { echo "Inserted $count records...\n"; } } } $pdo->commit(); fclose($file); echo "Import complete. Inserted a total of $count records.\n"; ?>