svcghost — 2011-06-26T00:43:35-04:00 — #1
What is more efficient in this example? PHP script inputs data into MySQL database. Should I first check to see if an identical record exists (don't want identical) with PHP by doing a SELECT statement? Or should I just set the field to be UNIQUE in MySQL and just let the user try to input identical data but fail thanks to MySQL?
I'm wondering which is more efficient. Using PHP to check via mysql_query every time seems exhaustive especially if users don't try and input identicals. But what if I just let MySQL reject the data? Is that a performance hogger? Or is this the ideal approach?
r937 — 2011-06-26T03:43:47-04:00 — #2
it's twice as efficient, as it does only one database call instead of two
immerse — 2011-06-26T08:52:27-04:00 — #3
As Rudy says, you can let it fail at the database.
Don't forget, however, to catch the database's error message so that you can tell the user why it failed.
svcghost — 2011-06-26T14:57:03-04:00 — #4
Good point! Thanks for the reminder.
cups — 2011-06-26T18:30:55-04:00 — #5
sbrainerd — 2013-06-07T16:55:58-04:00 — #6
Hey great info everyone - even though I know I'm a little late to the party. So far it works perfectly. What I'm trying to do is verify whether a street address has been previously inputted or not.
Checking UNIQUE works just fine when the address is all contained in one input box (on html form) and one row of the table, but I'd like to standardize it a bit more with street address, city, state, country, zip all having their own input box (on the html form).
I'm wondering what is the best practice for something like this. Would I have each html field as a populate a separate row in the table? Or could I combine all the data from the different input boxes (of the html form) and have them populate one row in the table?
Thanks in advance,