문제

I had a PHP application that automatically committed the insertion whenever I submitted the form. The code I used looked like this:

$dbConnection = new mysqli($hostname,$username,$password,$database);

$query = "some query here";
$queryResult = $dbConnection->query($query);
if($queryResult){
    $_SESSION['insertedID'] = $dbConnection->insert_id;
}
else{
}

As you can see, I used the auto_increment value as a SESSION variable. So, then I decided to add some commit/rollback protection to my application. So, I changed my code to look like this:

$dbConnection = new mysqli($hostname,$username,$password,$database);
$dbConnection->autocommit(FALSE);
$query = "some query here";
$queryResult = $dbConnection->query($query);
if($queryResult){
    $dbConnection->commit();
    $_SESSION['insertedID'] = $dbConnection->insert_id;
}
else{
    $dbConnection->rollback();
}

However, I have found out that the "$dbConnection->insert_id" function no longer works. Is this possible with commit/rollback in place, or am I out of luck? If this does not work with commit/rollback, is there some other way I can quickly grab the last auto_increment ID, or am I out of luck there too?

도움이 되었습니까?

해결책

Retrieve $dbConnection->insert_id BEFORE commit()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top