I am trying to set up my first transaction in MySQL using PHP / PDO ...
I have a quick question: what is the best way to determine if a previous request was successful or not? Here is what I have right now, but I would rather find a way to test the request using the if statement.
This is a pretty code layout to try to get a working model. I know that $ results does not test effectively if something is good or bad. I have more there, as a place for a real deal, when the time has come.
if ($_POST['groupID'] && is_numeric($_POST['groupID'])) { $sql = "SET AUTOCOMMIT=0"; $dbs = $dbo->prepare($sql); $dbs->execute(); $sql = "START TRANSACTION"; $dbs = $dbo->prepare($sql); $dbs->execute(); $sql = "DELETE FROM users_priveleges WHERE GroupID=:groupID"; $dbs = $dbo->prepare($sql); $dbs->bindParam(":groupID", $_POST['groupID'], PDO::PARAM_INT); $dbs->execute(); try { $sql = "DELETE FROM groups WHERE GroupID=:groupID LIMIT 1"; $dbs = $dbo->prepare($sql); $dbs->bindParam(":groupID", $_POST['groupID'], PDO::PARAM_INT); $dbs->execute(); $results["error"] = null; $results["success"] = true; try { $sql = "DELETE FROM users WHERE Group=:groupID"; $dbs = $dbo->prepare($sql); $dbs->bindParam(":groupID", $_POST['groupID'], PDO::PARAM_INT); $dbs->execute(); $results["error"] = null; $results["success"] = true; $sql = "COMMIT"; $dbs = $dbo->prepare($sql); $dbs->execute(); } catch (PDOException $e) { $sql = "ROLLBACK"; $dbs = $dbo->prepare($sql); $dbs->execute(); $results["error"] = "Could not delete associated users! $e"; $results["success"] = false; } } catch (PDOException $e) { $sql = "ROLLBACK"; $dbs = $dbo->prepare($sql); $dbs->execute(); $results["error"] = "COULD NOT REMOVE GROUP! $e"; $results["success"] = false; } }
php mysql pdo
guyfromfl
source share