note 120178 added to pdo.begintransaction

From: Date: Thu, 17 Nov 2016 09:29:24 +0000
Subject: note 120178 added to pdo.begintransaction
Groups: php.notes 
Request: Send a blank email to php-notes+get-207150@lists.php.net to get a copy of this message
When transaction gets your enemy: try { $pdo->beginTransaction(); // ... whatever db actions you have and then: $sql_insert = 'INSERT INTO a (a_id, a_name, a_date) VALUES (:a_id, :a_name CURRENT_TIMESTAMP)'; $stmt = $pdo->prepare($sql_insert); $stmt->bindValue(':_id', $id); $stmt->bindValue(':a_name', $name); $stmt->execute(); $pdo->commit(); } catch (\PDOException $e) { $pdo->rollBack(); http_response_code(500); header("Content-type: text/plain"); echo $e; exit(); } Notice the sytax error? There's a missing comma that just slips through every gate of surveillance: "...UES (:a_id, :a_name CURR..." :: between the to be binded value and current timestamp command. The missing comma will not generate any error no syntax error, no database error. Though this record will never or partially make it. This will just silently fail. The story gets rough if you build your other dependencies on the transactions unmovable foot-stones of trust. Just imagine you have some basic records in your transaction that you really wanna ensure ends up correctly in your db. And these records have a foreign constraint relation with another record. (That of course if you'd include in the transaction would throw you a big red error(–for who don't get it: transaction will first attempt to write everything into a temporary place, where it will get checked before everything gets into its permanent place, so constraints cannot be checked for something that don't exist yet.)) So you have a constraint that's based on the trust PDO will throw an error if something gets screwed, so what, you'll just place that insertion after the commit was successful. (Without any doublecheck.) What happens you will get some partial records in your database without a relation that just might be important. You will that way be able to build up very sophisticated strange behaviours in your application. So pay attention! ---- Server IP: 72.52.91.14 Probable Submitter: 89.134.204.104 ---- Manual Page -- http://php.net/manual/en/pdo.begintransaction.php Edit -- https://master.php.net/note/edit/120178 Del: integrated -- https://master.php.net/note/delete/120178/integrated Del: useless -- https://master.php.net/note/delete/120178/useless Del: bad code -- https://master.php.net/note/delete/120178/bad+code Del: spam -- https://master.php.net/note/delete/120178/spam Del: non-english -- https://master.php.net/note/delete/120178/non-english Del: in docs -- https://master.php.net/note/delete/120178/in+docs Del: other reasons-- https://master.php.net/note/delete/120178 Reject -- https://master.php.net/note/reject/120178 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#207150) next »