Issues with importing CSV file into MySQL when comma in address - php

I'm using a script that I found online to import CSV file into MySQL: http://www.johnboy.com/blog/tutorial-import-a-csv-file-using-php-and-mysql but when the script detects a comma in an address like this My address, CO 80113 in a cell, it also splits there as well.
I've seen a solution where you can save from excel to a Tab Delimited txt file. Then you go into notepad and replace the tabs with semicolons.
Is this the best practice for "fixing the comma in the address" issue?
My end goal is to take a CSV file from Highrise full of hundreds of clients into our MySQL database, then make updates every couple of months so this seems to be a decent script but am I going at this the wrong way?
EDIT : It appears that the part of the PHP code that splits up the cells is this
while ($data = fgetcsv($handle,1000,",","'"));

If you use comma as column separator, then you should quote all string field values, e.g. -
1,'My address, CO 80113'
2,'His address, CO 80114'
4,'Her address, CO 80115'
and so on
Try to use LOAD DATA INFILE statement with FIELDS ENCLOSED BY '\'' option.

comma (',') is the hart of CSV file so when you generate the csv file it divide cell when comma found.
So I think that you should replace the comma with the space or any other delimeter. you can use PHP 's default function str_replace() when your final code is generate for CSV.

Related

Insert in MYSQL from document table

I am using php to enter data from CSV file in mysql table. One column of CSV has text data for which I cannot maintain any formatting, and so the data when displayed in the webpage does not show up in a nice way.
I am thinking now to have the data in MS Word table and use php to insert data in mysql table from the Word table. Once again, the only advantage for me is to maintain the formatting of the data of one column.
How can I enter data from Word table in mysql table?
So according to the comments you need to format the text in the Description column using HTML.
This is not that easy task and can be very complex depending on the exact features of the formatting you need.
For example, to make new lines appear as new lines in HTML page, you will need to replace new line characters with <br /> tags:
$description = str_replace("\n", "<br />", $description);
If you need some more complex formatting like bulleted lists or italic/bold tags you will need to implement more complex parsing and formatting code.
For bulleted lists you will need to split the content into lines ($arr = explode("\n", $description)), find the lines starting with a character representing the bullet (* for example, depends on your column format) and replace them with <li> tags. Besides that you will need an <ul> and </ul> tags marking the start and the end of the list.
Similar approach will be needed for every formatting you need to keep in HTML.

Import CSV file into Mysql with Multiple Delimiters/Field Separators

I'm trying to import a large csv file into Mysql. Unfortunately, the data within the file is separated both by spaces and tabs.
As a result, whenever I load the data into my table, I end up with countless empty cells (because Mysql only recognizes one field separator). Modifying the data before importing it is not an option.
Here is an example of the data:
# 1574 1 1 1
$ 1587 6 6 2
$115 1878 8 9 23
(Where the second and third value of every row are separated by a tab)
Any ideas?
If my goal were just to import the file, i'd use sed -i 's/,/ /g' *.txt to create just one delimiter to worry about.
I like CSVs, but perhaps there's a string encased in double quotes that contains a comma or space, in which case this isn't perfect. It'd still import, just would modify those strings.
In that case, another approach I've used in production is Stat/Transfer. There's a syntax language to create a shell script to convert the file and specify multiple delimiters.
MySQL import CSV file using regex delimiter
Assuming you're using LOAD DATA INFILE try this:
load data local infile 'c:/somefile.txt' into table tabspace
columns terminated by ' '
(col1, #col23, col4, col5)
set col2 = left(#col23, instr(#col23,char(9))-1),
col3 = substr(#col23,instr(#col23,char(9))+1);
Note that the separator is a space so the second column contains the col2/col3 data. This is assigned to a variable #col23 which is then split up and the parts assigned to col2 and col3.

PHP Imported CSV data cut short when encounterng a comma

I have a simple PHP script that loops over data in a CSV file, and adds the records to the database accordingly. One of my fields is a description field, but that description field itself has a comma (or multiple comma's) in it. It seems as though data for that field is only read up until the comma, however the next field is correct, so it is not as though the field after that is the remainder of the description, is is using the next column which is right.
Am I supposed to escape the comma? I am adding this data to a MySQL database, could that be where the issue is being caused?
My SQL query could be something like:
$description = $data[7]; //description column eg: "hello, my name is xxxxx, I am old"
INSERT INTO tblsomething (id, description) VALUES ($id, '$description');
The above statement only inserts the description as "hello" and nothing after the first comma it encounters.
Any ideas why this is?
Many thanks,
Simon
EDIT: This is solved, apologies to all as it was a silly mistake. It appears that the person who did the front end was creating arrays of content using the patter ',' to split the content. IT seems that the description - although supposed to be one array entry - was being split into multiple entries due to it containing comma's. This will be solved by using a more rare character like the pipe symbol to create our separators.
Thanks to all
Because it's not a CSV file. Fields in a CSV file that contain commas are supposed to be delimited by double quotes; this way the CSV functions in PHP will handle them properly.

Convert MySQL insert from comma separated to piping

I have a problem to insert detail from one table into another in MySQL table. The compatibility differs as the one table accepts commas to separate detail in a column and the other table accepts piping. What PHP code can I use to convert the commas into piping?
Ok guys let me re-phrase. I am doing a data dump from another db to my db, the problem is that the content formats of the two tables aren't compatible, meaning that my one table uses UPPERCASE and the other one uses camelCase, and that the one uses (,) without spaces behind or infront where the other table uses (|) with spaces on both sides. I'm lost and need help
Take a look at http://php.net/manual/en/function.strtr.php
I found the answer:
$replace = str_replace(", "," | ", $result2);

How to read a bulk data feed via php?

I have a large file that I would like to read via php, and then insert various fields into MySQL.
Each file in the feed is in plain text format, separated into columns and rows. Each record has the same set of fields. The following are the delimiters for each field and record:
Field Separator (FS): SOH (ASCII character 1)
Record Separator (RS) : STX (ASCII character 2) + "\n"
If I look at the first few lines of the file they look like this:
#export_dateapplication_idlanguage_codetitledescriptionrelease_notescompany_urlsupport_urlscreenshot_url_1screenshot_url_2screenshot_url_3screenshot_url_4screenshot_width_height_1screenshot_width_height_2screenshot_width_height_3screenshot_width_height_4
#primaryKey:application_idlanguage_code
#dbTypes:BIGINTINTEGERVARCHAR(20)VARCHAR(1000)LONGTEXTLONGTEXTVARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(20)VARCHAR(20)VARCHAR(20)VARCHAR(20)
#exportMode:FULL
I am struggling to no where to start in order to read this file into PHP, can anyone help with the basic PHP to read each record, and assign a variable to each field, which I then will be able to write into MySQL. I can handle the writing into SQL once I have the various fields set up.
Thanks in advance,
Greg
files greator than 2gb cant be read in PHP (32 bit limit).
For lower size use simple fopen function
And inserting mysql is all the work of macthing patterns and inserts.
If structure of table is same every row then better make it manual once and then just execute inserts by extracting values either by regex or other functions like explode and split .
If every line has delimiters between each field, you may look at fgetcsv().
When you use fgetcsv() on a line, it will return an array with the contents from that line. Since you have several lines, put the funciton inside a while()-loop (look at example #1)

Categories