I
I
iOS Dav2015-12-12 05:47:46
MySQL
iOS Dav, 2015-12-12 05:47:46

How to properly use mysqli transactions (there is an example)?

how to properly use mysqli transactions?
you need to execute 2 requests in a transaction.
Or rather, execute the first one and read insert_id from it, which will be used in the second query.
it is right?
$login='jon';
$password='dsfsdf';
$reg_date=dete(dmy);
$mysqli= new mysqli('localhost', 'mysql', 'mysql', 'fortest');
$mysqli->set_charset('utf8');
$mysqli->autocommit(false);
$res=$mysqli->query('INSERT INTO `users`(`login`, `password`, `reg_date`) values ​​("'.$login.'","'.$password.'","' .$reg_date.'") ');
$new_id=$res->insert_id;
$res->free_result();
$a=$mysqli->query('INSERT INTO `top` values("'.$new_id.'") ');
$a->free_result();
$mysqli->commit();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question