A user may enter keywords into a text field and separate the keys using comma.
So the input may be bananas, apple, orange, pineapple.
In my database, I have a table called keyword, and it has only one column keyword which also is the primary key.
I add the keywords to the database, by $myArray = expload(',', $keywords).
Then I loop through the array and do a `INSERT INTO myTable’.
Now, if the keyword already exists, I will get an error message.
I can overcome the error message by using the INSERT IGNORE INTO statement. If the record is a duplicate, the IGNORE keyword tells MySQL to discard it silently without generating an error.
My question is: Is this a good way of doing it? Or should I first check to see if the keyword exists?
I’m kind of thinking two queries vs one. And will this affect server load?
insert ignore is great, it kills 2 birds with one stone.
insert ignore is theoretically non-standard sql, but it’s still very useful. if you ever need to go with some other storage engine, you can ameliorate those minor things if such an event ever comes to pass… no need to go through hoops to pre-port your code in this case.