I have the following code to import a CSV file in to a MYSQL database but am having a few issues on import.
$csv_file = CSV_PATH . "resultsimport.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile)) {
$csv_data[] = fgets($csvfile);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['ID'] = $csv_array[0];
$insert_csv['SerialNo'] = $csv_array[1];
$insert_csv['ChannelNo'] = $csv_array[2];
$insert_csv['IL850'] = $csv_array[3];
$insert_csv['IL1300'] = $csv_array[4];
$insert_csv['IL1310'] = $csv_array[5];
$insert_csv['IL1550'] = $csv_array[6];
$insert_csv['RL850'] = $csv_array[7];
$insert_csv['RL1300'] = $csv_array[8];
$insert_csv['RL1310'] = $csv_array[9];
$insert_csv['RL1550'] = $csv_array[10];
$query = "INSERT INTO results(ID,SerialNo,ChannelNo,IL850,IL1300,IL1310,IL1550,RL850,RL1300,RL1310,RL1550)
VALUES('".$insert_csv['ID']."','".$insert_csv['SerialNo']."','".$insert_csv['ChannelNo']."','".$insert_csv['IL850']."','".$insert_csv['IL1300']."','".$insert_csv['IL1310']."','".$insert_csv['IL1550']."','".$insert_csv['RL850']."','".$insert_csv['RL1300']."','".$insert_csv['RL1310']."','".$insert_csv['RL1550']."')";
$n=mysql_query($query, $connect );
$i++;
}
fclose($csvfile);
echo "File data successfully imported to database";
mysql_close($connect);
Here is an example of the first two lines from the CSV file:
ID,SerialNo,ChannelNo,IL850,IL1300,IL1310,IL1550,RL850,RL1300,RL1310,RL1550
1405230001-A-MTP-1-01,1405230001,A-MTP-1-1,0.320,0.150,,,,,,
As you can see there is only data in two of the columns, but on some imports there will be data to import in the final column.
The problem happens when there is no data in that final column, my import process adds in \r\n\ to the database.
How can I avoid that?
I also end up with a blank line on import at the end.
PHP 5 has a built in function to parse CSV for you, fgetcsv. It is better than relying on explode (which is naive about text enclosed with quotes that could contain commas. An empty line will result in an empty $data array, so test for that and skip them
while (($data = fgetcsv($csvfile, 1000, ",")) !== FALSE) {
if (empty($data) continue;
// rest of data manipulation continues
}
Sidenote: you should not be using the mysql_* family of functions. That extension has long been deprecated. You should be using mysqli or PDO to connect to your database and using prepared queries to guard against SQL injection attacks.
Related
I have quite a few CSV files that are unfortunately encoded with iso-8859-2 (according to Brackets). I would like to iterate over these files with PHP and convert them.
I found https://csv.thephpleague.com/9.0/converter/charset/ but the way I can use the conversion function is uncertain to me.
Their example code
use League\Csv\CharsetConverter;
$csv = new SplFileObject('/path/to/french.csv', 'r');
$csv->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY);
$encoder = (new CharsetConverter())->inputEncoding('iso-8859-15');
$records = $encoder->convert($csv);
This is my code so far that is part of a form to upload one file and save the contents to the database for testing. It of course saves the text in the incorrect format.
$db = ConnectDB::getConnection('address_dtb');
$sql = " ... ";
$stmt = $db->prepare($sql);
$rowCount = 0;
$temp_name = $_FILES['adresscsv']['tmp_name'];
$file_handle = fopen($temp_name, 'r');
while (($items = fgetcsv($file_handle, 1000, ';')) !== FALSE) {
if($flag) { $flag = false; continue; }
$stmt->execute($items);
$rowCount++;
}
fclose($file_handle);
ConnectDB::closeConnection($db);
What is the correct way to use the PHP CSV library above to iterate over locally saved files in a for loop to automate the process?
I ended up using iconv as hinted.
$files = glob('address/*.csv');
foreach ($files as $csv) {
$file_data = file_get_contents($csv);
$utf8_file_data = iconv('Windows-1250', 'UTF-8', $file_data);
file_put_contents($csv, $utf8_file_data);
}
You do not have to use a library. There is a function in PHP that can do that iconv
I have created a website which requires me to upload a csv file into the mysql database (Wamp server). Since I have never done this before a detailed answer with steps will be really helpful. I need the user to upload file using html input file option and then a php code to upload this file to mysql database. Iam using this code
<?php
$con=mysql_connect("localhost","","");
mysql_select_db("sg",$con);
define('CSV_PATH','C:/Users/mkutbudd/Desktop/');
$csv_file = CSV_PATH . "dum.csv";
if (($getfile = fopen($csv_file, "r")) !== FALSE) {
$data = fgetcsv($getfile, 1000, ",");
while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$result = $data;
$str = implode(",", $result);
$slice = explode(",", $str);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
$query = "INSERT INTO dummy(dum1,dum2,dum3)
VALUES('".$col1."','".$col2."','".$col3."')";
$s=mysql_query($query, $con );
}
}
}
echo "File data successfully imported to database!!";
mysql_close($con);
?>
Your first step is to allow user to upload a file, and then you need to handle the errors if there are, validate the file, and if everything is correct, then move the uploaded file to its place: http://php.net/manual/en/features.file-upload.php
Then you need to parse your file, with a php function for it: http://hu1.php.net/manual/en/function.fgetcsv.php
After this, you need to create a connection to the database: http://hu1.php.net/manual/en/book.mysqli.php
And then you need to insert your data into your table with a loop, like foreach on your array, what you created from fgetcsv: http://hu1.php.net/manual/en/control-structures.foreach.php
There are an alternative way, if you have right to run system or execute functions and you have right to run mysql command, then you can use the mysql load data command: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Done.
I have one csv file like this:
"Name","surname","note"
"Jhon","Jhon","fdskngkfngkfnkglnlksanjlbgsjangkjlvnkajsdf"
"Jhon2","Jhon2","fdssfsfsfkngkfngkfnkglnlksanjlbgsjangkjlvnkajsdf"
"Jhon3","Jhon3","fdssfsfsfkngkfngkfnk
glnlksanjlbgsjangkjlvnkajsdf"
"Jhon4","Jhon4","fdssfsfsf
kngkfngkfnkglnlks
anjlbgsjangkjlvnkajsdf"
I need to join the line who return, because i need to add this file in mySQL.
I use this code
$csv_file = "output.csv";
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode('","', $csv_data[$i]);
$insert_csv = array();
$insert_csv['Gestore'] = ltrim($csv_array[0], '"');
echo $insert_csv['Gestore'] . "</br>";
$query = "INSERT INTO prova(Gestore) VALUES('".$insert_csv['Gestore']."')";
$n=mysql_query($query, $connect );
$i++;
}
It works but i need to solve precedent problem...
You can simply use the PHP function fgetcsv.
As an alternative to PHP you could use
LOAD DATA INFILE
see MySQL manual, LOAD DATA INFILE, if you've got the FILE privilege.
i know how to upload a csv file into mysql database but only through cmd. i want to know how to upload csv file into mysql database using php form and will disregard some information on the excel and will only start importing starting from a certain line. ? kindly help me.
(PHP 4, PHP 5)
fgetcsv
See php manual http://php.net/manual/en/function.fgetcsv.php
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
Try this it's working well, you can add as many values as possible depending on the number of columns you have in the CSV file. Then in the HTML code put the uploading syntax in the tag.
**
$fname = $_FILES['csv_file']['name'];
$chk_ext = explode(".",$fname);
$filename = $_FILES['csv_file']['tmp_ name'];
$handle = fopen($filename, "r");
if(!$handle){
die ('Cannot open file for reading');
}
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
{
$query = "INSERT INTO tablename (col1_csv, col2_csv)
values ('$data[0]', '$data[1]');
mysql_query($query) or die(mysql_error ());
}
fclose($handle);
?>
**
You can use the MySQL LOAD DATA INFILE statement to bulk-insert thousands of records at once. PHP can handle the file upload. The PHP code would be something similar to:
$query = sprintf("
LOAD DATA LOCAL INFILE '%s'
INTO TABLE `table1`
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
",
mysql_real_escape_string($FILES["file1"]["tmp_name"])
);
The LOCAL keyword should allow you to workaround some security restrictions. Change the FIELDS TERMINATED BY and LINES TERMINATED BY parameter to match the separators used by excel while exporting. IGNORE 1 LINES tells MySQL to skip the header row(s).
Note: Excel does not seem to use an escape character; but it will (i) enclose the fields that contain , and " with " (ii) use "" to escape a single " inside data. I believe MySQL will understand this encoding and import the data correctly.
You could use the "LOAD DATA INFILE " statement with the " IGNORE ... LINES " option which you can use from the command line as well as from PHP.
try this:
$filename=$_FILES["upload_file"]["name"];
$extension = end(explode(".",$filename));
if ($extension=='csv') {
$tmp_file=$_FILES["upload_file"]["tmp_name"];
$handle = #fopen($tmp_file, "r");
//specify your own database connection parameter
$db = new PDO('mysql:host=localhost;dbname=demo','user','password');
$stmt = $db->prepare("INSERT INTO writers (writer_name, writer_email) VALUES (?, ?)");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$array=explode(",",$buffer);
$count=1;
foreach ($array as $value) {
$stmt->bindParam($count, $value);
$count++;
}
$stmt->execute();
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
$db = null;
echo "<p>Success</p>";
}
else {
$error="<p style='color:red;'>Invalid file type</p>";
}
Refer to http://pradipchitrakar.com.np/programming/upload-csv-mysql-php/
I have been working with a script to change my CSV file to Tab Delimited. It seems to work great, but it is read-only. How do I go about actually writing the changes to the file? Here is the script:
<?php
$myfile = "/path/to/my.csv";
$csv_fp = fopen ($myfile, "r");
$rows = 0;
while ($data = fGetCsv ($csv_fp, 10000, ","))
{
$num = count($data);
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
$rows++;
}fclose ($csv_fp);
?>
This is how you can change a comma delimited CSV to a tab delimited. However, I don't see the benefits of doing this. I think comma delimited CSVs are much less prone to errors.
<?php
$myfile = "/path/to/my.csv";
$csv_fread = fopen($myfile, 'r');
$rows = array();
while ($columns = fgetcsv($csv_fread, 10000, ",")) {
$rows[] = $columns;
}
fclose($csv_fread);
$csv_fwrite = fopen($myfile, 'w');
foreach($rows as $row){
fputcsv($csv_fwrite, $row, "\t");
}
fclose($csv_fwrite);
?>
You will need to change the mode in your call to fopen from r to one that opens the file in write mode. Which one you choose depends on what you want to do (append, overwrite). I am guessing that you want to overwrite, in which case you can choose w or w+.
Check out the manual for fopen and fwrite.