Home  >  Q&A  >  body text

I set up the transaction mechanism and deliberately set the second sql statement to be wrong. Why was the first sql statement executed in the end? Shouldn't the transaction be rolled back and not executed at all?


<?php

$pdo=new PDO('mysql:host=localhost;dbname=student','root','root');

try{

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pdo->beginTransaction();

//The first sql statement

$sqla="delete from chen where id=14";

$pdo->exec($sqla) ;

//The sql statement on the second day

$sqlb="delete froms chen where id=15"; //The from is written incorrectly on purpose

$pdo->exec($sqlb);


}catch(Exception $e){

$pdo ->rollBack();

$e->getMessage();

echo "Operation failed";

}

?>


陈老师陈老师2368 days ago1183

reply all(1)I'll reply

  • NULL

    NULL2018-05-21 10:55:04

    Is the database engine MyISAM? Try changing it to InnoDB

    reply
    1
  • 陈老师

    Yes, I found the problem. Thank you very much. Awesome.

    陈老师 · 2018-05-21 10:55:46
  • Cancelreply