4

I have the following code. I am trying to import a text file in to sql table using php as suggested by one of the users on this site.

Unfortunately my import got errors half way due to my text file having "Max and Min" words in the file.

I tried to find out what i can do to avoid it. Most of the stuff i found was about using reserved words in the column name. But mine is not a column name it is inserted in the columns as data.

Can this be avoided as I don't know how many other reserved words are present in the text file and I need to make my code run automatically everyday. I can't have it aborting every time. It is a huge text file so I can't manually replace keywords everytime either.

mysqli_query("CREATE TABLE IF NOT EXISTS `add_feature_id` (
`id_f` INT(10) unsigned NOT NULL AUTO_INCREMENT,
`id_product` INT(10) unsigned NOT NULL,
`id_feature` INT(10) unsigned NOT NULL, 
`value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL
DEFAULT   NULL,PRIMARY KEY ( `id_f` ) )",$conn);

$fd = fopen('trial.txt', 'r');
$fheader = fgets($fd); 

while (($data = fgetcsv($fd,0, "~")) !== FALSE) {
$id_product = $data[0];
$id_feature = $data[1];
$unitval = $data[2];
$value = mysql_real_escape_string($unitval);

mysqli_query("INSERT INTO   `add_feature_id`(`id_product`,`id_feature`,`value`) 
VALUES ($id_product,$id_feature,'$value')",$conn) or die(mysql_error());
}

fclose($fd);

$result = mysqli_query("SELECT * FROM `add_feature_id`",$conn);
//I print my result here but i get error while insert is executed

The error i am getting is

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 ' Max and Min Requirements

Low entry level requirement Minimu' at line 2

This is the part of my text file where the error occurs Max and Min Requirements as can be seen in the text file below

IMSKU~AttributeID~Value~Unit~StoredValue~StoredUnit(header row)

1006854 ~ 16257 ~Licensing Program: Max and Min Requirements<ul><li>Low entry level  requirement</li><li>Minimum 1 server (Band S) OR 5 desktop (Band A)</li></ul> ~  ~ 0.00 ~
User56756
  • 352
  • 4
  • 19
  • What is the created SQL statement that triggers this error? Dump them all before calling `mysql_query()`. And by the way: The mysql extension is deprecated and will be removed from PHP. Please switch to mysqli or PDO - otherwise you will not be able to run with PHP 7. – Sven Jul 13 '15 at 00:35
  • Yes I need to change it to mysqli but if this does not work that that won't either so I plan to get this code working and than will change it to mysqli later. Thanks for that tip. How can i dump all the errors? – User56756 Jul 13 '15 at 00:37
  • Is it possible that one of `$id_product`, `$id_feature`, `$unitval` has a single quote (or isn't an integer?). Can you find out where in the file that it's failing and edit that part of trial.txt into your question? If you can't find out where, can you post your entire trial.txt? Or just link to it. – Dave Chen Jul 13 '15 at 00:58
  • @DaveChen here is the link to my text file. http://toorakcomputerservices.com.au/PI-Detail-ASXX.txt – User56756 Jul 13 '15 at 01:17

2 Answers2

2

You've got invalid data on line 32325:

1062708~16257~Express Licensing Program:<ul><li>Targeted at small - medium companies (1-500 units)</li><li>Minimum purchase requirements for licenses</li><li>Includes all Symantec software products</li><li> Certificated-based program - requires no legal review</li><li>Band identified via number of units per transaction</li></ul>
<br />Max and Min Requirements<ul><li>Low entry level requirement</li><li>Minimum 1 server (S-Band) OR 5 desktop (A-H Band)</li></ul>~~0.00~
1062708~16260~2~~0.00~

That middle line.

The reason it fails is because $id_product and $id_feature aren't in single quotes/or escaped.

What you should do is prepare/execute this (or get rid of that invalid data).

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • I put `id_product` and `id_feature` in a single quote, It started importing data fine for some time but now i am getting another error. Notice: Undefined offset: 1 in /home/myfile.php on line 32 Notice: Undefined offset: 2 in /home/myfile.php on line 33, my line 32 and 33 are $id_feature = $data[1]; $d = $data[2]; – User56756 Jul 13 '15 at 02:05
  • I tried this http://stackoverflow.com/a/17456614/4237767 and it worked. Well the import worked perfectly. It did not execute the select statement and echo the table. But atleast the sql table is working fine. – User56756 Jul 13 '15 at 02:33
1

How about quote encapsulated strings with:

LOAD DATA INFILE '/tmp/trial.txt' 
INTO TABLE add_feature_id  
    FIELDS TERMINATED BY ',' 
           OPTIONALLY ENCLOSED BY '"'
    LINES  TERMINATED BY '\n' -- or \r\n

https://dev.mysql.com/doc/refman/5.1/en/load-data.html

Edit:

create table fred
(
fullName varchar(255) not null,
age int not null
);

fred2.txt:

"jason"~50
"fred max min max smith"~87
"jason avg abs sum smith"~12


load data infile 'c:\\dev\\fred2.txt'
into table fred
fields terminated by '~'
optionally enclosed by '"'
lines terminated by '\r\n'

select * from fred

yep they came in :)
Drew
  • 24,851
  • 10
  • 43
  • 78
  • In my text file I the fields are terminated by ~ so I should replace ',' with that right? Also am I supposed to change the '/tmp/' directory name too?? I have not used LOAD before so i am wondering. – User56756 Jul 13 '15 at 00:56
  • Yes to the above, give it a shot in a meaningless table to test. The link provides flexible syntax for skipping row1 (header row ) etc – Drew Jul 13 '15 at 00:59
  • It is not working for me. I think i am doing something wrong. Do i need to create a table with all the columns and its data restrictions before LOAD is executed? – User56756 Jul 13 '15 at 01:27
  • 1
    In other words do my fred thing – Drew Jul 13 '15 at 01:34
  • I will give it a go. Maybe my file location was the problem. – User56756 Jul 13 '15 at 02:34