Reputation: 2194
I'm using Codeigniter and the Active Record class to build a simple API in PHP. I got a table where an IP Adress works as a primary key, and here's my question:
Is it better to run a query against the Database to check if the IP exists when the function to insert the data is run, and then perform a second query inserting the data (if the IP doesnt exists) OR Try to insert the Data and parse the Error message for "Duplicate Entry" or something. So i'm only using 1 DB query with this one.
So its basically PHP Validation vs MySQL Validation. So which technique is better and/or faster.
Thanks.
Upvotes: 1
Views: 367
Reputation: 791
Consider the use of ON DUPLICATE KEY UPDATE syntax. Read more about this at: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html But if it's not preferable in your case than i would go with php validation because triggering an error in mysql is never a good idea.
Upvotes: 1