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.

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

i am using 'id' as primary key, what should be my query in that case?
I fixed it! by using this query: REPLACE INTO table (id) VALUES ('1');
you can add one more UNIQUE KEY on table to avoid duplicate values.
Oh okay I will do this, 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?
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.

2 Comments

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?
@NomanRizwan You should probably create a new question and ask there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.