false, 'error' => 'Database SQL file not found.']; } try { $sql = file_get_contents($sql_file); // Split SQL by semicolon, but be careful with multi-line statements if any // For simplicity, we can use PDO exec for the whole file if it's well-formed // Or split and execute parts. // Using exec on the whole block is often okay for small/medium dumps $pdo->exec($sql); return ['success' => true, 'message' => 'Database initialized successfully.']; } catch (PDOException $e) { return ['success' => false, 'error' => 'Database import failed: ' . $e->getMessage()]; } } // If run from CLI or specifically requested if (php_sapi_name() === 'cli' || isset($_GET['run'])) { $res = setup_database(); if (php_sapi_name() === 'cli') { echo ($res['success'] ? "SUCCESS: " . $res['message'] : "ERROR: " . $res['error']) . "\n"; } else { header('Content-Type: application/json'); echo json_encode($res); } }