|
-
May 28th, 2009, 01:31 PM
#1
[RESOLVED] PDO update query executes but nothing happens
Hello
I do not know if I'm stupid, or crazy. I have a function that executes a select query to a db and gets some parameter. Then I do some operations with files where I get some new value. Then I run an update query on the same registry to insert a new parameter.
The thing is that both queries throw no error, the code after the second one executes well but the table stays without the update, keeping the initial value...
Here is the code. Just to make it more understandable: it's a image files indexation resort, and the parameter I update is the primary image property that one of the images in the directory can hold. So if some file was "primary" and its index changes from for ex. 5 to 2 - I have to update the DB so the property would appear correctly in the web forms.
PHP Code:
public function getDBParameter($dbTable,$key,$keyValue,$field){ $param = NULL; try{ $this->openDbPDO($this->p); $stmt = $this->conn->prepare("SELECT ".$field." FROM ".$dbTable." WHERE ".$key." = ?"); $stmt->execute(array($keyValue)); if($row = $stmt->fetch()){ $param = $row[$field]; } $this->conn = null; }catch(PDOException $e){ $this->log->lwrite($e->getMessage()); } return $param; }
public function updateDBParameter($dbTable,$key,$keyValue,$field,$value){ try{ $this->openDbPDO($this->p); $stmt = $this->conn->prepare("UPDATE ".$dbTable." SET ".$field." = ? WHERE ".$key." = ?"); $stmt->execute(array($value,$keyValue)); $this->conn = null; $this->log->lwrite("updated"); //this line executes }catch(PDOException $e){ $this->log->lwrite($e->getMessage()); //the function never enters here } }
public function resortImages($usherId){ $lib = new Repository(); $images = $lib->listUsherImages($usherId); //returns array of pairs of remaining images $prevPriImg = NULL; $postPriImg = NULL; if(count($images)>0){ //getting previous primary image index $prevPriImg = $this->getDBParameter("usher_stats","idUsher",$usherId,"idPrimaryImg"); //resorting images... $idxs = array(); for($i=0; $i<count($images); $i=$i+2){ $idxs[$i/2] = substr($images[$i],14,1); //the 15th char in file names is the index } if($handle = opendir("images/data/usher/".$usherId."/")){ $k = 0; while(false !== ($file = readdir($handle))){ if($k>1){ for($i=0; $i<count($idxs); $i++){ if(strcmp($idxs[$i],substr($file,14,1))==0){ rename("images/data/usher/".$usherId."/".$file, "images/data/usher/".$usherId."/".substr_replace($file,"$i",14,1)); if(strcmp($prevPriImg,substr($file,14,1))==0){ $postPriImg = $i; //getting new index for primary image $this->log->lwrite("CHANGING PriImg: ".$prevPriImg." ---> ".$postPriImg); //the line above gives the right values } } } } $k++; } closedir($handle); } //setting updated primary image index $this->updateDBParameter("usher_stats","idUsher",$usherId,"idPrimaryImg",$postPriImg); } return count($images)/2; }
I've done all this thousands of times. There are dozens of similar queries and methods altering the DB in this project, all run with no problems. The method resortImages() logging and testing showed that all files are resorted well, the $prevPriImg and $postPriImg change their values accordingly, and by the moment I run updateDBParameter() to apply the update the $postPriImg has the correct value. The only problem in all this - the table never gets updated. So the problem is in the updateDBParameter() method, maybe some PDO connection problems, I do not know... The most unexplainable thing is that the $this->log->lwrite("updated"); line executes, and the algorithm never enters catch part. Also, if I intentionally put some error into the update query the PDOException writes the related error as it should.
This is the line where the divine magic happens I suppose: $stmt->execute(array($value,$keyValue));
Searching in the Web I've found this: http://bugs.mysql.com/bug.php?id=36406. Though this is not entirely my case...
Last edited by Xeel; May 28th, 2009 at 03:02 PM.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
May 29th, 2009, 05:10 PM
#2
Re: PDO update query executes but nothing happens
I was stupid after all... topic closed
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
June 1st, 2009, 10:00 AM
#3
Re: PDO update query executes but nothing happens
For the sake of possible solutions for others, can you post what you did, or changed?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|