简体   繁体   中英

LOAD DATA LOCAL INFILE

i have a XML File i want to insert data from that XML file to the Database

if tried with the following code but its not storing data in datatase

$sql = "LOAD DATA INFILE 'corporatenews/$zname' INTO TABLE `corporatenews` FIELDS TERMINATED BY '|' LINES STARTING BY '<row>' TERMINATED BY '</row>' (`ByLine`,`StoryId`,`PublishDate`,`Author`,`Category`,`SubCategory`,`TitleLine`,`SubTitleLine`,`StoryMain`,`Flag`)";

$zname is the variable that stores the xml file name

i tried to catch error by mysql_error but nothing showed up

It looks like you may be overcomplicating things by trying to shove the file directly into MySQL. For a simple solution you may be able to read the entire XML file into memory w/ file_get_contents , then just insert it into MySQL from PHP.

EDIT: Per OP request for clarification.

OK, well there are a bunch of ways to parse an XML file in PHP, the easiest way IMO is SimpleXML . Basically, you want to read in the file, and then iterate over it creating a bunch of INSERT statements. It could be quicker to batch the insert statements, but forget about that for now.

The code would be something like this (this code assumes the MySQL connection is setup properly):

// iterate over XML elements
$oXml = new SimpleXMLElement('path/to/file.xml', 0, true);
foreach($oXml as $oElement) {
  // build main part of query
  $sSql = 'INSERT INTO (`ByLine`,`StoryId`,`PublishDate`,`Author`,`Category`,`SubCategory`,`TitleLine`,`SubTitleLine`,`StoryMain`,`Flag`) VALUES (';

  $sSql .= $oXml['ByLine'] . ',';
  $sSql .= $oXml['StoryId'] . ',';

 // ..

}

Basically you use SimpleXML to extract the data from the XML attributes / child nodes, then you build up a string you can pass to mysql_query .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM