patternphpMinor
Cron PHP script to pull newly added clients from remote db, add their information to local db
Viewed 0 times
scriptlocalcronpulladdedphpclientsinformationtheirremote
Problem
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
-
Now create an entry into the customers table and import data from the users table. For
-
Update the time last checked table value to reflect the signup timestamp of the most recent customer.
And here is what I have written:
```
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 whe
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 the id 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:
```
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 whe
Solution
I don't have a lot of time, So I'm just going to list a couple of things that come to mind:
lastIndertIddoes not work on all database, and i could return a wrong result
$stmt1doesn't tell me much. Use variable names that are self-explaining. It will help you in the long run.
- If you use prepared statements, atleast use them correctly. You are using prepare, but still injecting the variables into the query directly. Instead of using addParam, ...
- On the prepared statements, only create the prepared statement once, and then execute it multiple times inside the foreach. This is where prepared statements are made for.
- don't use
endofreachbut use curly brackets. Always use curly brackets. It just adds for better readability.
- Don't define your configurations. Have them in an array. Much cleaner. Maybe even wrap the PDO creation in a function.
- When creating a PDO object, you can pass in attributes as 4th parameter. Do that instead of calling the
setAttribute.
- use less line-endings. You go a little crazy on the line-endings. Maybe read up on the php-fig coding standards. This kind of tends to produce easy to read code.
Context
StackExchange Code Review Q#80332, answer score: 3
Revisions (0)
No revisions yet.