0

I am getiing simple data from a txt file. Text file consist of list of countries. For e.g: America England Africa I am getting the content, explode it on line break and inserting into mysql. Till here, everything is going fine. Now what I actually want is that if i add two more countries in that list of txt file. So the data already got inserted should not insert again and only the two new lines should be added just.

Noman Rizwan
  • 1
  • 1
  • 1
  • 3

2 Answers2

1

If you have a PRIMARY KEY or UNIQUE KEY on a table then you can use INSERT IGNORE clause as:

e.g. If you have a UNIQUE KEY on column country then do:

INSERT IGNORE INTO table (country) VALUES ('America');

to forcefully remove duplicates see here

Community
  • 1
  • 1
Omesh
  • 27,801
  • 6
  • 42
  • 51
0

array_unique() is your friend here.

ex.

$filecontents = '...';
$countries = explode("\n", $filecontents);
$countries = array_unique($countries);

Then you can add the countries to your database. If you are talking about data that's already in the database do what @Omesh said.

Meisam Mulla
  • 1,845
  • 3
  • 23
  • 37
  • Yes REPLACE INTO done my work, now what I need is i delete any country from textfile so it should delete the entire row from mysql. How could it is possible? – Noman Rizwan Jul 26 '12 at 05:50
  • @NomanRizwan You should probably create a new question and ask there. – octern Jul 26 '12 at 06:06