Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mysqli_commit does not work when using mysqlnd + MYSQLI_ASYNC + autocommit=false. The main transaction gets commited, but everything executed via MYSQLI_ASYNC does not get commited.
<?php
/**
* Linux packer-virtualbox-iso 3.11.0-15-generic #25~precise1-Ubuntu SMP Thu Jan 30 17:39:31 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
* PHP 5.5.19-1+deb.sury.org~precise+1 (cli) (built: Nov 19 2014 19:32:34)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
*/
/*
CREATE TABLE `asynctest` (
`date` date NOT NULL,
`user_id` int(10) unsigned NOT NULL
) ENGINE=InnoDB;
*/
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = 'xxxx';
$dbbase = 'dbname';
if (!defined('MYSQLI_ASYNC')) {
die('Only with mysqlnd driver');
}
$insert = function($autocommit) use ($dbhost, $dbuser, $dbpass, $dbbase) {
$i = mysqli_init();
mysqli_options($i, MYSQLI_OPT_CONNECT_TIMEOUT, 3);
$connected = mysqli_real_connect($i, $dbhost, $dbuser, $dbpass, $dbbase, 3306, '', MYSQLI_CLIENT_INTERACTIVE);
if (!$connected || mysqli_connect_errno()) {
die('Check connection');
}
$i->autocommit($autocommit);
$i->set_charset('utf8mb4');
$sql = "INSERT INTO asynctest (`date`, `user_id`) values ('" . date('Y-m-d') . "', '" . time() . "')";
echo $sql . "\n";
mysqli_query($i, $sql , MYSQLI_ASYNC);
if ($sqlerror = mysqli_error($i)) {
die('SQL ERROR: ' . $sqlerror);
}
if ($w = mysqli_warning_count($i) > 0) {
die('SQL WARNINGS: ' . $w);
}
mysqli_commit($i);
mysqli_close($i);
unset($i);
echo "Insert done\n";
};
$select = function() use ($dbhost, $dbuser, $dbpass, $dbbase) {
$x = mysqli_init();
mysqli_options($x, MYSQLI_OPT_CONNECT_TIMEOUT, 3);
$connected = mysqli_real_connect($x, $dbhost, $dbuser, $dbpass, $dbbase, 3306, '', MYSQLI_CLIENT_INTERACTIVE);
if (!$connected || mysqli_connect_errno()) {
die('Check connection');
}
$x->autocommit(true);
$x->set_charset('utf8mb4');
$someresult = @mysqli_query($x, "SELECT COUNT(*) FROM asynctest");
if ($sqlerror = mysqli_error($x)) {
die('SQL ERROR: ' . $sqlerror);
}
if ($w = mysqli_warning_count($x) > 0) {
die('SQL WARNINGS: ' . $w);
}
$data = mysqli_fetch_all($someresult, MYSQLI_ASSOC);
mysqli_free_result($someresult);
print_r($data);
mysqli_close($x);
unset($x);
};
echo "Doing insert AUTOCOMMIT=FALSE: \n";
$insert(FALSE);
echo "Doing select: \n";
$select();
echo "Doing insert AUTOCOMMIT=TRUE: \n";
$insert(TRUE);
echo "Doing select: \n";
$select();
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.