Hi again! I'm hoping some here have experience with Python as well as PHP. When I google "PHP versus Python speed", I get mixed and obviously biased responses. I'm not about to start building web pages in Python, but I have a PHP script that I run from the Linux shell and I was wondering if I should consider converting it to Python for better speed.
The script takes raw vendor data from a query to MS SQL and cleans it and imports it into MySQL. Further, it backs up and restores email addresses for these vendors in MySQL (because they aren't stored in the MS SQL database). The script runs once a week in order to pull new customer data into MySQL.
Here's the PHP script:
// Create a log file...
$errFile = "/inet/www/Temp/ach_vm_log.txt";
if(file_exists($errFile)) unlink($errFile);
$efh = fopen($errFile, 'x');
fwrite($efh, "*** ACH Vendor Import Log File ***\n\n");
// First, drop temp table and back up the current email addresses...
$query = "DROP TABLE IF EXISTS VM_Emails_Temp";
$result = mysql_query($query, $link)
or die("Error dropping email table...".$query);
$query = "CREATE TABLE VM_Emails_Temp SELECT company_code, vendor_code, email FROM Vendor_Master WHERE email <> '';";
$result = mysql_query($query, $link)
or die("Error creating email table...".$query);
// Clear out the current vendor table...
$query = "DELETE FROM Vendor_Master";
$result = mysql_query($query, $link)
or die("Error deleting vendor data...".$query);
// Log any SQL errors...
$err = "DELETE QUERY RESULTS:\n".mysql_error()."\n".$query."...done\n\n";
fwrite($efh, $err);
// Open data file...
$dataFile = "/tmp/ach_vm_results.csv";
$fh = fopen($dataFile, 'r');
$i = 1;
fwrite($efh, "INSERT QUERY RESULTS:\n");
while($Data = fgets($fh))
{
// Clean up the string data...
$Data = clean_string($Data);
$Data = str_replace("'", "", $Data);
$Data = str_replace("\n", "", $Data);
$Data = str_replace(",", "", $Data);
$Data = str_replace("\t", ",", $Data);
// Load string data into an array...
$str_array = explode(",", $Data);
// Clean and load array data into field variables...
$company_code = clean_string($str_array[0]);
$vendor_code = clean_string($str_array[1]);
$vendor_name = clean_string($str_array[2]);
$address1 = $str_array[3];
$address2 = clean_string($str_array[4]);
$address3 = clean_string($str_array[5]);
$state = clean_string($str_array[6]);
$zip = clean_string($str_array[7]);
$phone = clean_string($str_array[8]);
// Try to insert record...
$query = "INSERT INTO Vendor_Master (company_code, vendor_code, vendor_name, address1, address2, address3, state, zip, phone) ".
"VALUES ('$company_code', '$vendor_code', '$vendor_name', '$address1', '$address2', '$address3', '$state', '$zip', '$phone')";
$result = mysql_query($query, $link);
// Log any errors...
$err = "Vendor record $i) ".mysql_error()."...done\n";
fwrite($efh, $err);
$i++;
}
// Merge saved email addresses from VM_Email_Temp...
$query = "UPDATE Vendor_Master, VM_Emails_Temp SET Vendor_Master.email = VM_Emails_Temp.email WHERE ".
"Vendor_Master.company_code = VM_Emails_Temp.company_code AND Vendor_Master.vendor_code = VM_Emails_Temp.vendor_code ";
$result = mysql_query($query, $link)
or die("Error merging email addresses...".$query);
fclose($fh);
// Write totals to error log...
$err = "\n------------------------------\n\n".$i." lines written to ACH table successfully!";
fwrite($efh, $err);
fclose($efh);
print("Job complete.");