You are here: Re: UPDATE multiple fields in one statement « PHP SQL « IT news, forums, messages
Re: UPDATE multiple fields in one statement

Posted by Hilarion on 10/14/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

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация