A bigger question is, “Do I need to worry about doing this?”
This applies to a Bad Words function I wrote. In it, I not only replace any bad words, but I capture all bad words from the user’s post and place them into an array. From there, I want to permanently log them int he database, so I have a record of offenders and can take action on people that consistently have potty mouths!
Hopefully my Bad Words function grows cobwebs. And when it is needed, I would hope that an offending post might only have a few curse words in it.
That beings aid, do I need to get fancy and do just one INSERT, or is a simple FOREACH loop with multiple INSERTs good enough?
(Note: The hope is that my client’s website becomes very popular - like SItePoint - and will have hundreds (or thousands) of people posting comments throughout the day!)
When possible a single trip to the database is the most scalable and optimized approach for data entry. Though to often than not it is not nearly as practical as ideal. However, in your case I wouldn’t see why not. Especially if you’re not using any type of abstraction layer that doesn’t support this feature.
if you’re not restricted to an interface/api than that is a terrible way to do things considering it will result in n * 2 trips to the database server where n is equal to the number of fruits. On the other hand using a single insert query will only ever result in two trips.
You’re right but 1 + n is *likely to be SIGNIFICANTLY more than 2. A *little foresight like this can drastically reduce performance degradation when application(s) scale. Also Ic an’t remember the last time I worked on an application where the database wasn’t on a separate server. I would think most production systems are though I’m not a system admin.
So based on the problem I described, and the constraint of needing to use Prepared Statements, how would you recommend I do things?
For now, I just flipped the code above, and stuck my INSERT Prepared Statement inside of a FOREACH loop. It works, but is someone swears a lot, or I have tons of people swearing, then I will keep my database busy!!
I’m not sure if it is such a big deal for something like this, but I am always trying to learn how to program things more efficiently!