alex
alex

Reputation: 490203

Is this so bad when using MySQL queries in PHP?

I need to update a lot of rows, per a user request. It is a site with products.

I could...

What is the usual practice here?

Thanks

Upvotes: 4

Views: 168

Answers (3)

Michas
Michas

Reputation: 9428

The best practice is probably to use stored procedures.
http://dev.mysql.com/doc/refman/5.5/en/stored-routines.html
It would be only one command on PHP side. The rest would be implemented on MySQL sever.

You can also try to execute few queries at once using mysqli::multi-query.
http://php.net/manual/en/mysqli.multi-query.php

Upvotes: 0

bobince
bobince

Reputation: 536369

Just do the updates. In a transaction if you need to. 15 updates is peanuts, unless you're doing it on every page access or something.

You don't want to be deleting/re-inserting rows just to avoid extra queries. And you won't be able to, if you ever want to have a foreign key referencing the table you're updating.

Almost certainly a premature optimisation.

Upvotes: 2

Dean Harding
Dean Harding

Reputation: 72658

15 UPDATEs is really not all that many. If you were talking about 15 hundred then perhaps you'd have to think about your design a bit more...

Upvotes: 2

Related Questions