Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL LOAD DATA INFILE Help

Tags:

php

mysql

I just can't seem to get this query right. Basically I'm taking a csv from a form and trying to load it into the database. I took the majority of the query from phpmyadmin. i should be using the temp file right? here it is...

<form name = "price_chart" method="post" action="index.php?option=<?php echo $option ?>&task=complete" enctype="multipart/form-data">
<label>File: </label>
<input type="file" name="white" id="white"/><br />
<input type="hidden" value="TEST" name = "test" />
<input type="submit" name = "upload" value="Upload File" />
</form>

<?php
}
function complete($option){
$pfile = $_FILES['white']['name'];  
$ptmpName = $_FILES['white']['tmp_name'];
$test = $_POST['test'];
$result = mysql_query("LOAD DATA LOCAL INFILE '$ptmpName' REPLACE INTO TABLE 'jos_rates_table' FIELDS TERMINATED BY ',' ENCLOSED BY ' ' ESCAPED BY '\\' LINES TERMINATED BY '\n'('country' , 'rate')")or die ('Error: '.mysql_error ());
        while ($row = mysql_fetch_array($result)) {
        }
        $num_rows = mysql_num_rows($result);
        echo $num_rows;
}

UPDATE: Here's the Error message

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''jos_rates_table' FIELDS TERMINATED BY ',' ENCLOSED BY ' ' ESCAPED BY '\' LINES ' at line 1

like image 370
Greg Thompson Avatar asked Jul 26 '26 21:07

Greg Thompson


1 Answers

You need to escape the slash for the lines terminated clause, otherwise you just added a newline to your query.

LINES TERMINATED BY '\\n'

Mysql will just get \n, but PHP tries to interpret those so you have to escape them.

You may have other issues, but you haven't told us what happens.

like image 193
Fosco Avatar answered Jul 29 '26 11:07

Fosco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!