0

i have a problem with transaction, because if I try to enter a wrong query, the system should not enter either.

Instead the system inserts the first query, and the second is not, and does not enter the Rollback

This is my code:

$conn = connect();

try {
     /* set autocommit to off */
     $conn->autocommit(FALSE);

    /* MY QUERY */
    $conn->query("INSERT INTO `transazioni` (`id`, `nome`, `numero`) VALUES (NULL, 'luca', '12')");
    $conn->query("I--NSERT INTO `transazioni` (`id`, `nome`, `numero`) VALUES (NULL, 'paolo', '12')");

   /* commit transaction */
   $conn->commit();

    } catch (Exception $e) {
      // faccio rollback
      $conn->rollback();
      echo "enter in rollback";
}           

How can i fix it ?

Thanks Andrea

user2663914
  • 65
  • 1
  • 6

1 Answers1

0

Your problem is caused by mysql engine. MyISAM doesn't support transactions, you have to change to InnoDB.

Also note that your catch block woudn't work unless you configure mysqli for that explicitly.

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345