<?php
$servername = "localhost";
$username = "tostonc_zx3";
$password = "A1s2d3f4!@#";
$dbname = "tostonc_zx3_test";



$allClientsfile = "data/contacts.json"; //Get existing JSON file

$jsondata = file_get_contents($allClientsfile); //Get data from existing JSON file

$arr_data = json_decode($jsondata, true); // converts existing JSON data into an Array

$serialized_array = serialize($arr_data);

var_dump($arr_data); // gives back a string, perfectly for db saving!

echo "<br>";

$size = sizeof($arr_data); //get total number of clients in the exiting client array and display
$row = $size;
echo "<BR> Existing Contact Array Size: " . $size . "<BR>";

$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

// prepare sql and bind parameters
$stmt = $pdo->prepare("INSERT INTO SMSContacts (clientID, firstname, lastname, initials, type, email, phone, notes, localdatetime )
          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? )");

try {
    $pdo->beginTransaction();
    foreach ($serialized_array as $row)
    {
        $stmt->execute($row);
    }
    $pdo->commit();
}catch (Exception $e){
    $pdo->rollback();
    throw $e;
}

?>
