PDO's automatic submission function is enabled on MySQL with autocommit turned off
<p>I was just assigned to a legacy PHP codebase that only used PDO. </p><p>The connection is done using the following pattern: </p><p><br /></p>
<pre class="lang-php prettyprint-override"><code>// Connect to MySQL database
$con = new mysqli($host, $user, $password, $database);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$con->autocommit(true);
</code></pre>
<p>And using that autocommit(true) works as expected even if the MySQL server is configured with SET autocommit = 0. </p><p>Now I'm trying to migrate from normal SQL to prepared statements, so I wrote this</p><p><br /></p>
<pre class="lang-php prettyprint-override"><code>$dsn = "mysql:host=$host;dbname=$database";
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
</code></pre>
<p>and the example function below</p>
<pre class="lang-php prettyprint-override"><code>function updateTeam($pdo, $name, $id)
{
try {
$data = [
'name' => $name,
'id' => $id
];
$sql = "UPDATE teams SET name = :name WHERE id=:id";
$stmt = $pdo->prepare($sql);
$stmt->execute($data);
// $pdo->commit();
}
catch (PDOException $e) {
db::$pdo->rollback();
error_log('Failed: ' . $e->getMessage() );
}
catch (Exception $exc) {
db::$pdo->rollback();
error_log('Failed: ' . $exc->getMessage());
}
}
</code></pre>
<p>Do not submit. I have to uncomment $pdo->commit(); to make it work, which I don't want because it forces me to change every query statement in the application. </p><p>What did I miss in PDO configuration? I tried var_dump($pdo->query('SELECT @@autocommit')->fetchAll()); and the result was </p><p><code></code>< /p>
<pre class="brush:php;toolbar:false;">array(1) { [0]=> array(2) { ["@@autocommit"]=> string(1) "0" [0]=> string(1) "0" } }</pre>
<p><br /></p>