Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8731979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:21:20+00:00 2026-06-13T09:21:20+00:00

I understand why we should not loop MySQL queries and I am trying to

  • 0

I understand why we should not loop MySQL queries and I am trying to avoid it. I have an array $names which contains a list of keywords to insert into someTable, with a UNIQUE constraint on the MySQL field name:

mysql> select * from someTable where name='stress';
+-----+--------+
| id  | name   |
+-----+--------+
| 143 | stress |
+-----+--------+
1 row in set (0.00 sec)

mysql> insert into someTable (name) values ('stress');
ERROR 1062 (23000): Duplicate entry 'stress' for key 'name'
mysql>

I would prefer a query of the following type to avoid a loop:

$result = mysql_query("insert into someTable (name) values ('firstName'), ('secondName'), ('thirdName')");

However, if any of the names already exist then none of the values get inserted. Furthermore, the value of $result will be FALSE. How can I construct a single query to insert the values, which will not fail if any of them exist, and which will return TRUE if any of them were inserted?

I know that I could write a stored procedure for this, but that would just move the loop from PHP to MySQL. Other than bandwidth (not critical as this is on localhost) that doesn’t seem to save anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:21:21+00:00Added an answer on June 13, 2026 at 9:21 am
    INSERT IGNORE INTO someTable (name) values ('firstName'), ('secondName'), ('thirdName')
    

    If there is a duplicate, it will ignore and continue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to better understand when I should and should not use Iterators.
I'm a newcomer to Python but I understand that things should not be done
This should really be allowed - I do not understand why it is not.
I don't understand why this is not working. It should be very easy. upload.php
http://code.google.com/appengine/docs/python/tools/uploadingdata.html is not clearly understand. Where i should call the bulkloader.py or appcfg.py? Should
I know from the codeing guidlines that I have read you should not do
Below is a code which I am not understand. #include<stdio.h> int main(int argc, char
I understand that I should not optimize every single point of my program, let's
I have a block of uncommented code I'm trying to understand. The comments are
I'm trying to make a loop that will recurse through an array of bytes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.