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 DATASELECT INTO OUTFILEThe SELECT INTO OUTFILE SQL statement is actually a variant of the SELECT query. It’s used when youwant to direct query output to a text file. This file can then be opened by a spreadsheet application, orimported into another database like Microsoft Access, Oracle, or any other software that supportsdelimitation. Its general syntax format follows:SELECT [SELECT OPTIONS] INTO OUTFILE filenameEXPORT_OPTIONSFROM tables [ADDITIONAL SELECT OPTIONS]The following list summarizes the key options:• OUTFILE: Selecting this option causes the query result to be output to the text file.The formatting of the query result is dependent upon how the export options areset. These options are introduced below.• DUMPFILE: Selecting this option over OUTFILE results in the query results beingwritten as a single line, omitting column or line terminations. This is useful whenexporting binary data such as a graphic or a Word file. Keep in mind that youcannot choose OUTFILE when exporting a binary file, or the file will be corrupted.Also, note that a DUMPFILE query must target a single row; combining outputfrom two binary files doesn’t make any sense, and an error will be returned if youattempt it. Specifically, the error returned is, “Result consisted of more than onerow.”• EXPORT OPTIONS: The export options determine how the table fields and lines willbe delimited in the outfile. Their syntax and rules match exactly those used in LOADDATA INFILE, introduced earlier in this chapter. Rather than repeat thisinformation, please see the earlier section “Importing Data with LOAD DATAINFILE” for a complete dissertation.Usage TipsThere are several items worth noting regarding use of SELECT INTO OUTFILE:• If a target file path is not specified, the directory of the present database is used.• The executing user must possess the selection privilege (SELECT_PRIV) for thetarget table(s). Further, the user must possess the FILE privilege because thisquery will result in a file being written to the server.• If a target file path is specified, the <strong>MySQL</strong> daemon owner must possess adequateprivileges to write to the target directory.• The process leaves the target file world-readable and -writeable, an unexpectedside effect. Therefore, if you’re scripting the backup process, you’ll probably wantto change the file permissions programmatically once the query has completed.• The query will fail if the target text file already exists.• Export options cannot be included if the target text file is a dump file.729

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

Saved successfully!

Ooh no, something went wrong!