11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 38 • IMPORTING AND EXPORTING DATAThis command results in the compression and transmission of the data found in the local text file(C:\audit\inventory.txt) to the table inventory located in the company database. Note that mysqlimportstrips the extension from each text file and uses the resulting name as the table into which to import thetext file’s contents.Writing a mysqlimport ScriptSome years ago, I was involved in the creation of a corporate web site for a pharmaceutical corporationthat, among other things, allowed buyers to browse descriptions and pricing information for roughly10,000 products. This information was maintained on a mainframe, and the data was synchronized on aregular basis to the <strong>MySQL</strong> database residing on the web server. To accomplish this, a one-way trust wascreated between the machines, along with two shell scripts. The first script, located on the mainframe,was responsible for dumping the data (in delimited format) from the mainframe and then pushing thisdata file via sftp to the web server. The second script, located on the web server, was responsible forexecuting mysqlimport, loading this file to the <strong>MySQL</strong> database. This script was quite trivial to create, andlooked like this:#!/bin/sh/usr/local/mysql/bin/mysqlimport --delete --silent \--fields-terminated-by='\t' --lines-terminated-by='\n' \products /ftp/uploads/products.txtTo keep the logic involved to a bare minimum, a complete dump of the entire mainframe databasewas executed each night, and the entire <strong>MySQL</strong> table was deleted before beginning the import. Thisensured that all new products were added, existing product information was updated to reflect changes,and any products that were deleted were removed. To prevent the credentials from being passed in viathe command line, a system user named productupdate was created, and a my.cnf file was placed in theuser’s home directory, which looked like this:[client]host=localhostuser=productupdatepassword=secretThe permissions and ownership on this file were changed, setting the owner to mysql and allowingonly the mysql user to read the file. The final step involved adding the necessary information to theproductupdate user’s crontab, which executed the script each night at 2 a.m. The system ran flawlesslyfrom the first day.Loading Table Data with <strong>PHP</strong>For security reasons, ISPs often disallow the use of LOAD DATA INFILE, as well as many of <strong>MySQL</strong>’spackaged clients like mysqlimport. However, such limitations do not necessarily mean that you are outof luck when it comes to importing data; you can mimic LOAD DATA INFILE and mysqlimportfunctionality using a <strong>PHP</strong> script. The following script uses <strong>PHP</strong>’s file-handling functionality and a handyfunction known as fgetcsv() to open and parse the delimited sales data found at the beginning of thischapter:727

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!