Skip to content

Instantly share code, notes, and snippets.

@marklocklear
Created August 16, 2021 14:42
Show Gist options
  • Save marklocklear/da925c519a84665bac0ece71fa11e9f3 to your computer and use it in GitHub Desktop.
Save marklocklear/da925c519a84665bac0ece71fa11e9f3 to your computer and use it in GitHub Desktop.
<?php
$servername = "localhost";
$username = "admin";
$password = "password";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo 'Begin Import of Patients ' . "\n";
$di = new RecursiveDirectoryIterator('/Desktop/small_set');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
// echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
$info = pathinfo($file);
if($info["extension"] == "csv"){
// echo $filename;
$handle = fopen($filename, "r");
fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
// var_dump($data);
$dob = date("Y-m-d", strtotime($data[2]));
// echo "test: ". $dob;
// echo " firstname: " . $data[0]. " lastName " . $data[1]. " DOB " . $dob. "\n";
$sql = "SELECT id, fname, lname, DOB FROM `patient_data` WHERE fname = '$data[0]' AND lname = '$data[1]' AND
DOB = '$dob'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Record found for " . "id: " . $row["id"]. " - Name: " . $row["fname"]. " " . $row["lname"]. $row["DOB"].
" ". " skipping this record...". "\n";
}
} else {
echo "0 results found for ". " firstname: " . $data[0]. " lastName " . $data[1]. " DOB " . $dob.
" begining insert..." . "\n";
$sql = "INSERT INTO patient_data(fname, lname, DOB, phone_home, email, street, city, state, postal_code)
VALUES ('$data[0]', '$data[1]', '$dob', '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]')";
if ($conn->query($sql) === TRUE) {
echo "New paient created successfully" . "\n";
} else {
echo "Error: " . $sql . $conn->error . "\n";
}
}
}
}
}
$conn->close();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment