|
Posted by Hilarion on 09/29/37 11:29
> Thank you both. I made an update() function, and call it once for each
> different field. It works fine now.
You should NOT use separate UPDATE statements if you are updating
fields of same record (or records). J.O. Aho was referring to multiple
SQL statements (affecting different records in different ways) in one
execution.
If you are updating a record (or a set of records in a same way) then
you should - if it's possible (but it is in most cases) - apply changes
to all affected fields.
So do not do this (it is slower and MORE error prone):
$query = "UPDATE product SET price=$newprice WHERE ASIN = $product_asin";
execute_query_in_some_way( $query );
$query = "UPDATE product SET tracking_url='$newtracker' WHERE ASIN = $product_asin";
execute_query_in_some_way( $query );
but do this (it's faster and still not hard to debug and maintain):
$query = "UPDATE product SET price=$newprice,tracking_url='$newtracker'
WHERE ASIN = $product_asin";
What J.O. Aho was refering to was this (not recomended by him and sometimes
also impossible and in most cases hard to debug):
$query = "UPDATE product SET price=$newprice1,tracking_url='$newtracker1'
WHERE ASIN = $product_asin1;
UPDATE product SET price=$newprice2,tracking_url='$newtracker2'
WHERE ASIN = $product_asin2;";
execute_query_in_some_way( $query );
against this (recomended by him due to better debugging):
$query = "UPDATE product SET price=$newprice1,tracking_url='$newtracker1'
WHERE ASIN = $product_asin1";
execute_query_in_some_way( $query );
$query = "UPDATE product SET price=$newprice2,tracking_url='$newtracker2'
WHERE ASIN = $product_asin2";
execute_query_in_some_way( $query );
This one above can (and probably should) use some generalization, array,
function and/or loop.
Hilarion
[Back to original message]
|