Commit and Rolback Mysqli

Asked

Viewed 30 times

0

Guys, I’m using PHP’s Mysqli class and I came across the following situation...

I have two tables, the table A is the main and the table B is the daughter until there is ok, what I need to do is commit treatment and rollback to avoid failures in the future this way I put together the following script...

$DB->mConnect();
$DB->Mysqli->autocommit(false);

$er = true;

$query = "insert into (descricao) values ('testes')";
$er = !$DB->Mysqli->query($query) ? false : true;
$codigo = $DB->Mysqli->insert_id;

$query2 = "insert into (cod_pai, descricao) values (".$codigo.", 'teste filho')";
$er = !$DB->Mysqli->query($query2) ? false : true;

$er ? $DB->Mysqli->commit() : $DB->Mysqli->rollback();

var_dump($err);

Now comes my question, the daughter table B is dependent on the auto increment code that is generated in table A, what is occurring is that the way I did is not being generated the Code, and this way when will use in table B tells that the code of table A does not exist.

I wish I knew what I did wrong?

I appreciate help!

  • Manually entering the database, the AUTO_INCREMENT works? Checks if the column cod_pai and the others with I.A. are like, INT in the BD. Changes the variable inside the vardump, err does not exist. By convention, always use SQL clauses in uppercase (SELECT, INSERT, etc).

  • Does the code load to the end? Check whether the data is written to DB (except id)?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.