I wrote a PHP script and was wondering if it was well written or if it could be improved upon to read or run more efficiently, any help or critique would be greatly appreciated.
The file will be placed in cron jobs and run every 15 minutes. The goal is that it will take new customers from a remote MySQL database and add them to my local database with entries for both customers and users.
These are the general steps:
Return a list of new customers from the remote database by selecting all customers who have been added since the last update.
For every new customer, create an entry into the users table and import the customer's data from the remote database. Also to note the
user_id
created (auto incremented) as later it will be used to link the user to their corresponding column in the customers table.Now create an entry into the customers table and import data from the users table. For
primary_user_id
, reference theid
saved above. Note the new incremented customer_id.Update the time last checked table value to reflect the signup timestamp of the most recent customer.
And here is what I have written:
<?php
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "username");
defined('DB_PASS') ? null : define("DB_PASS", "password");
defined('DB_NAME') ? null : define("DB_NAME", "database");
defined('DB_REMOTESERVER') ? null : define("DB_REMOTESERVER", "remote.server");
defined('DB_REMOTEUSER') ? null : define("DB_REMOTEUSER", "user");
defined('DB_REMOTEPASS') ? null : define("REMOTEPASS", "pass");
defined('DB_REMOTENAME') ? null : define("DB_REMOTENAME", "db");
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null : define('SITE_ROOT', DS.'var'.DS.'www'.DS.'menu');
defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
$db1 = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS);
$db1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db2 = new PDO('mysql:host='.DB_REMOTESERVER.';dbname='.DB_REMOTENAME.';charset=utf8', DB_REMOTEUSER, DB_REMOTEPASS);
$db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Retrieve timestamp from most recent customer db update.
$smt = $db1->prepare("
SELECT time_last_checked FROM clients_update;
");
$smt->execute();
$data = $smt->fetchAll();
//Set time_last_checked to variable last_customer_entry_time, this will be used in the next query which selects all new customers added since this timestamp.
foreach ($data as $row):
$last_customer_entry_time = $row["time_last_checked"];
endforeach
//Select values from customers added since last_customer_entry_time
$smt1 = $db2->prepare("
SELECT first_name, last_name, phone, email, signup_date FROM customers where signup_date >= '".$last_customer_entry_time."';
");
$smt1->execute();
$data = $smt1->fetchAll();
foreach ($data as $row):
//Take new customer values from remote server db (db2) and insert into the users table in db1
$smt2 = $db1->prepare("
INSERT INTO `database`.`users`
(`email`, `name`, `last_name`, `phone`, `date_created`, `date_updated`)
VALUES (
'".$row["email"]."',
'".$row["first_name"]."',
'".$row["last_name"]."',
'".$row["phone"]."',
CURDATE(),
CURDATE()
);
");
$smt2->execute();
//Set signup date of customer to variable newtimestamp. When the last customer is added, this value will update time_last_checked
$newtimestamp = $row["signup_date"];
//Set the last auto incremented id value to a variable in order to insert the new customer's user_id in the customers table
$id = $db->lastInsertId();
//Take values from users table and insert them into customers table
$smt3 = $db1->prepare("
INSERT INTO `database`.`customers`
(`primary_user_id`, `customer_name`, `date_created`, `date_updated`)
VALUES (
'".$id."',
'".$row["first_name"]." ".$row["last_name"]."',
CURDATE(),
CURDATE()
);
");
$smt3->execute();
//Update users table with new customer ID from customers table
$customer_id = $db1->lastInsertId();
$smt4 = $db1->prepare("
UPDATE users SET customer_id = '".$customer_id."' ORDER BY user_id DESC LIMIT 1;
");
$smt4->execute();
//Update time last checked table value to reflect the signup timestamp of the most recent customer
$smt5 = $db->prepare("
UPDATE clients_update SET time_last_checked = '".$newtimestamp."';
");
$smt5->execute();
endforeach
?>