ckrudelux Posted September 4, 2012 Share Posted September 4, 2012 <?php $pdo = new PDO(...);$insert = $pdo->prepare(...);$update = $pdo->prepare(...); $items = array(...); foreach($items as $item) if($item->id) $update->execute(...); else $insert->execute(...); The value of $insert becomes TRUE while $update becomes a PDOStatement object.Why doesn't both of them become a PDOStatement? Link to comment Share on other sites More sharing options...
birbal Posted September 4, 2012 Share Posted September 4, 2012 both of them should return pdostatement object on success. how did you test it? can you post the exact code? Link to comment Share on other sites More sharing options...
ckrudelux Posted September 4, 2012 Author Share Posted September 4, 2012 (edited) both of them should return pdostatement object on success. how did you test it? can you post the exact code? This is the exact code but it should be the same as the example. private function updateChildren(Menu $menu){ if(count($menu->getItems())){ if($insert = DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')") && $update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id")){ foreach($menu->getItems() as $item){ if($item->getID()){ if($update->execute(array("label"=>$item->getLabel(),"path"=>$item->getPath(),"id"=>$item->getID()))){ $this->updateChildren($item); } }else{ if($insert->execute(array("label"=>$item->getLabel(),"path"=>$item->getPath()))){ $item->__construct(intval(DB::PDO()->lastInsertID())); $this->updateChildren($item); } } } } } if(count($menu->getRemoved())){ foreach($menu->getRemoved() as $remove) $this->removeMenu($remove); } } I tested it by var_dump() the insert and update variables Edited September 4, 2012 by ckrudelux Link to comment Share on other sites More sharing options...
justsomeguy Posted September 4, 2012 Share Posted September 4, 2012 This line: if($insert = DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')") && $update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id")){ Is going to set $insert to be the result of the logical && operator. It parses that line like this: if($insert = (DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')") && $update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id"))){ you want it like this: if(($insert = DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')")) && ($update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id"))){ Link to comment Share on other sites More sharing options...
ckrudelux Posted September 4, 2012 Author Share Posted September 4, 2012 This line: if($insert = DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')") && $update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id")){ Is going to set $insert to be the result of the logical && operator. It parses that line like this: if($insert = (DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')") && $update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id"))){ you want it like this: if(($insert = DB::PDO()->prepare("INSERT INTO `menu` (`label`,`path`,`parent`) VALUES (:label, :path, '". intval($menu->getID()) ."')")) && ($update = DB::PDO()->prepare("UPDATE `menu` SET `label`=:label, `path`=:path WHERE id=:id"))){ Oh.. should have thought about that.. thank you very much Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now