Shell Scripting with PHP...
Submitted by phrax on Thu, 2005-01-27 13:05.
Hall of Fame | Bad Architecture
David sent this in last week. It is part of a database migration shell script written in PHP. It looks like the script moves data from one table to another. Of course it does it in one of the most worst ways imaginable.
<?php
$sth = $sconn->prepare($sql);
$res = $sconn->execute($sth);
$fp = fopen("./tmp.emaillist","w");
while($tmp = $res->fetchRow()){
print "hi";
$data = "$tmp[0]\t$tmp[1]\t$tmp[2]\t$tmp[3]\t$tmp[4]\t$tmp[5] ... \t$tmp[56]\t$tmp[57]\t$tmp[58]\n";
fputs($fp,$data);
}
fclose($fp);
$cmd = "mysql -e \"load data local infile './tmp.emaillist' into table account\" --password=$dpass --database=$ddb";
?>
I had to trim down the $data = " ... "
line so that it fits. It was really, really long and fairly pointless. A few of things that make this WTF exceptionally bad:
- The use of an OOP DB abstraction layer. There must be an
include()
, or arequire()
somewhere that pulls in the library. Not grossly bad, but PHP shell scripts should be kept as individual as possible. - Dumping DB data into an external tab separated file first...
- then running another shell command to read the data into the database.
- Using "LOAD DATA LOCAL INFILE", it's use is discouraged, and disabled by default. That likely explains why the programmer had to dump to a text file and run the MySQL CLI client to get LOAD DATA INFILE to work. I smell workaround. Yuck!
A better way to do this? Perhaps using a single INSERT ... SELECT from MySQL.