Jump to content

Read txt file and insert into MySQL


Murciano

Recommended Posts

I need to read from a file and insert the files content into a MySQL column the table contains 3 fields

 

id INT

version VAR

xmlver LONGTEXT

 

What im wanting to do is grab the file, read it and then write it to a txt file, then the data from the txt file be read and sent to MySQL along with the id and xmlver details, i have seen this code snippet but when i try it it doesnt get me anywhere (confuses me)

mysql_query('insert into tablename (columnname) values ("' . mysql_real_escape_string(file_get_contents('somefile')) . '");');

 

 

Whats the best way to do this?, code below (is it worth writing the XML to txt before insertion or can i insert the xml as is?)

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
   if (isset($_POST['version'])) {
       
       $version = $_POST['version'];
       
       $data = file_get_contents("datafeed.xml");
       $data = file_put_contents('datafeed.txt', $data);       
       file_put_contents('version.txt', $version);

}
?>
<p align="center">
<input type="text" name="version" value="<?php include("version.txt"); ?>"/>
<input type="submit" name="<?php echo $version; ?>" value="Update Server" />
</p>

</form>

Link to comment
Share on other sites

Thanks, i have been looking at the LOAD DATA INFILE query earlier today, but am a little stumped at how to code it, would i need to run 2 mysql queries?.. 1 with the version and id and the other with LOAD DATA or could i put it all in one query?, if it can be done in 1 query would this be right

 

<?php
mysql_query("INSERT INTO file id='$id', version='$version' LOAD DATA INFILE 'datafeed.txt' INTO TABLE file.xmlver
");
       $result = mysql_query($query);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.