3v4l.org

run code in 300+ PHP versions simultaneously
<?php // 実行 $testNestedTransactions = new TestNestedTransactions(); $testNestedTransactions->createTables(); // CREATE TABLE $result = $testNestedTransactions->insert(); // INSERT echo "<p>{$result}</p>"; // 結果 // データベース接続 final class Database { private static array $instances = []; // データベース接続 final public static function getInstance(string $name): \PDO { if (!isset(self::$instances[$name])) { // データベース接続情報 $config = [ 'db1' => [ 'dsn' => 'mysql:host=localhost;dbname=mydb1', 'username' => 'myuser1', 'password' => 'mypassword1' ] ]; if (!isset($config[$name])) { throw new \DomainException(json_encode_noesc_slashes([ 'message' => 'Invalid database name: ' . $name ])); } $dbConfig = $config[$name]; $dsn = $dbConfig['dsn']; $username = $dbConfig['username']; $password = $dbConfig['password']; self::$instances[$name] = new \PDO($dsn, $username, $password); } return self::$instances[$name]; } } // 入れ子のトランザクション final class TestNestedTransactions { private \PDO $pdo; public function __construct() { $this->pdo = Database::getInstance('db1'); } // テーブル作成 final public function createTables(): void { try { $this->pdo->query(" CREATE TABLE IF NOT EXISTS test_table1 ( id INT AUTO_INCREMENT PRIMARY KEY, column1 VARCHAR(255) NOT NULL ) "); $this->pdo->query(" CREATE TABLE IF NOT EXISTS test_table2 ( id INT AUTO_INCREMENT PRIMARY KEY, column2 VARCHAR(255) NOT NULL ) "); } catch (\PDOException $e) { echo $e->getMessage(); } } // 入れ子のトランザクションで INSERT を実行 final public function insert(): string { try { // 外側のトランザクション開始 $this->pdo->beginTransaction(); // test_table1 の INSERT を実行 $stmt1 = $this->pdo->prepare("INSERT INTO test_table1 (column1) VALUES (:value1)"); $value1 = 'some_value_1'; $stmt1->bindParam(':value1', $value1); $stmt1->execute(); try { // 入れ子のトランザクション開始 $this->pdo->beginTransaction(); // test_table2 の INSERT を実行 $stmt2 = $this->pdo->prepare("INSERT INTO test_table2 (column2) VALUES (:value2)"); $value2 = 'some_value_2'; $stmt2->bindParam(':value2', $value2); $stmt2->execute(); // 入れ子のトランザクションのコミット $this->pdo->commit(); } catch (\Exception $e) { // 入れ子のトランザクションのロールバック $this->pdo->rollBack(); throw $e; } // 外側のトランザクションのコミット $this->pdo->commit(); return "成功"; } catch (\Exception $e) { // 外側のトランザクションのロールバック $this->pdo->rollBack(); // この行でエラーが発生 return "エラー: " . $e->getMessage(); } } }
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
Fatal error: Uncaught PDOException: could not find driver in /in/HivEd:36 Stack trace: #0 /in/HivEd(36): PDO->__construct('mysql:host=loca...', 'myuser1', Object(SensitiveParameterValue)) #1 /in/HivEd(49): Database::getInstance('db1') #2 /in/HivEd(4): TestNestedTransactions->__construct() #3 {main} thrown in /in/HivEd on line 36
Process exited with code 255.
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.1.0 - 8.1.33
Fatal error: Uncaught PDOException: could not find driver in /in/HivEd:36 Stack trace: #0 /in/HivEd(36): PDO->__construct('mysql:host=loca...', 'myuser1', 'mypassword1') #1 /in/HivEd(49): Database::getInstance('db1') #2 /in/HivEd(4): TestNestedTransactions->__construct() #3 {main} thrown in /in/HivEd on line 36
Process exited with code 255.

preferences:
114.66 ms | 409 KiB | 5 Q