[PHP] update count
What is the best way to only do an update if it going to update only one row? I want to protect my code so that it won't accidentally update more than one row. I can do a select first but there must be an easier way. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] update count
[snip] What is the best way to only do an update if it going to update only one row? I want to protect my code so that it won't accidentally update more than one row. I can do a select first but there must be an easier way. :-) [/snip] Do the update with conditions (this is a SQL question) UPDATE `tblFOO` SET `bar` = 'a_value' WHERE `barID` = '227098762' will only update the record(s) where barID is 227098762 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] update count
Bob Lockie wrote: What is the best way to only do an update if it going to update only one row? I want to protect my code so that it won't accidentally update more than one row. I can do a select first but there must be an easier way. :-) UPDATE thisDB.thisTable SET thisTable.thisColumn = 'thisValue' WHERE thisTable.uniqueColumn = 'someUniqueValue' -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] update count
On Wed, 16 Jun 2004 09:40:52 -0400, Bob Lockie [EMAIL PROTECTED] wrote: What is the best way to only do an update if it going to update only one row? You don't say what database you're using. The general way is to use a unique key in the WHERE clause. UPDATE tablename SET foo=1 WHERE id=123 MySQL also lets you use a LIMIT clause that will either limit the number of rows affected by the update, or matched by the WHERE clause depending on the MySQL version. UPDATE tablename SET foo=1 WHERE bar=2 LIMIT 1 -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] update count
What is the best way to only do an update if it going to update only one row? You don't say what database you're using. The general way is to use a unique key in the WHERE clause. UPDATE tablename SET foo=1 WHERE id=123 MySQL also lets you use a LIMIT clause that will either limit the number of rows affected by the update, or matched by the WHERE clause depending on the MySQL version. UPDATE tablename SET foo=1 WHERE bar=2 LIMIT 1 -robin The LIMIT clause is perfect but is that standard SQL? I am using MySQL right now but it will be run on another database eventually. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] update count
If your table has a primary key, or auto increment field works well and is quick. What is the best way to only do an update if it going to update only one row? I want to protect my code so that it won't accidentally update more than one row. I can do a select first but there must be an easier way. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php