Skip to content

Instantly share code, notes, and snippets.

@suin
Created August 25, 2011 07:17
Show Gist options
  • Save suin/1170149 to your computer and use it in GitHub Desktop.
Save suin/1170149 to your computer and use it in GitHub Desktop.
【混ぜるな!危険】mysql_connectとPDOを併用した場合、トランザクションが機能しないです
<?php
/*
CREATE TABLE `transaction_test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
$connection1 = mysql_connect('localhost', 'root', 'root');
mysql_select_db('test');
$pdo = new PDO("mysql:host=localhost; dbname=test", "root", "root");
mysql_query('BEGIN');
mysql_query("INSERT INTO transaction_test SET name = 'via mysql_connect'");
$pdo->query("INSERT INTO transaction_test SET name = 'via PDO'");
mysql_query('ROLLBACK');
$result = mysql_query('SELECT * FROM transaction_test');
while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
{
var_dump($row);
}
/*
array(2) {
["id"]=>
string(1) "2"
["name"]=>
string(7) "via PDO"
}
*/
mysql_close($connection1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment