horde, phplib, php4, mysql: problem solved
On Tue, 18 Jul 2000, Ivan E. Moore II wrote:
> arg...your not the only one who is seeing this...it's very bizarre and I have
> not been able to figure out what's causing it. Well, I should say, the
> one incident of it that I've seen. Have you exited out of your browser
> and tried again? If not, try that...if you still see the same results than
> I'm up to 2 strange occurances...
OK, on the php website I found out that mysql_affected_rows is causing
troubles anyway, so I wrote a workaround:
What do you think about this code? Could there be a problem that it's
nonatomic?
Thanks for any suggestions to improve this beast...
regards
Alex
--- ct_sql.inc.orig Sun Apr 9 21:50:33 2000
+++ ct_sql.inc Tue Jul 18 19:08:12 2000
@@ -8,6 +8,9 @@
##
## $Id: ct_sql.inc,v 1.1.2.1 2000/04/09 19:50:33 chuck Exp $
##
+## modified by Alexander List <alexlist@sbox.tu-graz.ac.at> 2000/07/18
+## to work around MySQL problems with mysql_affected_rows (al)
+##
## PHPLIB Data Storage Container using a SQL database
##
@@ -71,6 +74,12 @@
$name = addslashes($name);
$now = date("YmdHis", time());
+
+ # here, we look if there's already a session open with sid/name (al)
+ $tquery = sprintf("select * from %s where sid='%s' and name='%s'",
+ $this->database_table,
+ $id,
+ $name);
$uquery = sprintf("update %s set val='%s', changed='%s' where sid='%s' and name='%s'",
$this->database_table,
$str,
@@ -84,14 +93,28 @@
$str,
$now);
- # This may look wierd, but SELECTing first and then doing the
- # right thing would nonatomic (in MySQL, where we cannot SELECT
- # FOR UPDATE). This is nonatomic, too, but keeps the window small.
- $this->db->query($uquery);
- if ( $this->db->affected_rows() == 0
- && !$this->db->query($iquery)) {
- $ret = false;
+
+ # now if there's a session open, update the record, else insert a new
+ # one, if that fails, return false
+ # I know this isn't atomic, but I couldn't imagine another way
+ # working around affected_rows (al)
+ $tresult=$this->db->query($tquery);
+ if ($this->db->num_rows($tresult) <> 0) {
+ $this->db->query($uquery);
+ }
+ elseif (!$this->db->query($iquery)) {
+ $ret = false;
}
+
+# this is the original code I left for reference (al)
+// # This may look wierd, but SELECTing first and then doing the
+// # right thing would nonatomic (in MySQL, where we cannot SELECT
+// # FOR UPDATE). This is nonatomic, too, but keeps the window small.
+// if ( $this->db->affected_rows() == 0
+// && !$this->db->query($iquery)) {
+// $ret = false;
+// }
+
return $ret;
}
Reply to: