[phplib-dev] cvs commit From: athompso (phplib-dev <email protected>)
Date: 05/03/00

From: athompso
Date: Wed May 3 19:23:56 2000
Modified files:
      php-lib-stable/CHANGES
      php-lib-stable/php/ct_sql.inc

Log message:
Added workaround for MySQL UPDATE affected_rows() gotcha.
Thanks to Massimo Santoli <saint <email protected>> for suggestion.

Index: php-lib-stable/CHANGES
diff -u php-lib-stable/CHANGES:1.3 php-lib-stable/CHANGES:1.4
--- php-lib-stable/CHANGES:1.3 Tue Apr 25 08:04:55 2000
+++ php-lib-stable/CHANGES Wed May 3 19:23:54 2000
@@ -1,4 +1,10 @@
-$Id: CHANGES,v 1.3 2000/04/25 06:04:55 kk Exp $
+$Id: CHANGES,v 1.4 2000/05/03 17:23:54 athompso Exp $
+
+03-May-2000 athompso
+ - Added workaround to ct_sql.inc for case where MySQL UPDATE statements
+ return affected_rows()==0 if the UPDATE didn't actually change any
+ data but matched a row nonetheless.
+ Thanks to Massimo Santoli <saint <email protected>> for providing suggestion.
 
 22-Apr-2000 kk
   - Tracked changes to template.inc and cart.inc from master repository.
Index: php-lib-stable/php/ct_sql.inc
diff -u php-lib-stable/php/ct_sql.inc:1.1 php-lib-stable/php/ct_sql.inc:1.2
--- php-lib-stable/php/ct_sql.inc:1.1 Mon Apr 17 18:40:09 2000
+++ php-lib-stable/php/ct_sql.inc Wed May 3 19:23:55 2000
@@ -6,7 +6,7 @@
 ##
 ## Copyright (c) 1998,1999 Sascha Schumann <sascha <email protected>>
 ##
-## $Id: ct_sql.inc,v 1.1 2000/04/17 16:40:09 kk Exp $
+## $Id: ct_sql.inc,v 1.2 2000/05/03 17:23:55 athompso Exp $
 ##
 ## PHPLIB Data Storage Container using a SQL database
 ##
@@ -82,6 +82,12 @@
       $now,
       $id,
       $name);
+ $squery = sprintf("select count(*) from %s where val='%s' and changed='%s' and sid='%s' and name='%s'",
+ $this->database_table,
+ $str,
+ $now,
+ $id,
+ $name);
     $iquery = sprintf("insert into %s ( sid, name, val, changed ) values ('%s', '%s', '%s', '%s')",
       $this->database_table,
       $id,
@@ -90,9 +96,23 @@
       $now);
 
     $this->db->query($uquery);
+
+ # FIRST test to see if any rows were affected.
+ # Zero rows affected could mean either there were no matching rows
+ # whatsoever, OR that the update statement did match a row but made
+ # no changes to the table data (i.e. UPDATE tbl SET col = 'x', when
+ # "col" is _already_ set to 'x') so then,
+ # SECOND, query(SELECT...) on the sid to determine if the row is in
+ # fact there,
+ # THIRD, verify that there is at least one row present, and if there
+ # is not, then
+ # FOURTH, insert the row as we've determined that it does not exist.
+
+ if ( $this->db->affected_rows() == 0
+ && $this->db->query($squery)
+ && $this->db->f(1) == 0
+ && !$this->db->query($iquery)) {
 
- if ( $this->db->affected_rows() == 0
- && !$this->db->query($iquery)) {
         $ret = false;
     }
     return $ret;

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>