3v4l.org

run code in 300+ PHP versions simultaneously
<?php // 1件のコメントをINSERTする function insert_comment_single( $comment, $is_table_lock ){ try { $dbh = db_open(); if ( $is_table_lock ) { // テーブルロックを実行 $sql = 'LOCK TABLES tbl_comments WRITE, tbl_relations WRITE, tbl_tags WRITE'; $db->query( $sql ); } // INSERTする $sql = 'INSERT ...'; $db->query( $sql ); // エラー確認 if ( 何かエラーなら ) throw new Exception('何かエラーです'); // エラーなければコミット if ( $is_table_lock ) $dbh->commit(); // 戻り値 $result = [ 'status' => 'ok', 'inserted_id' => 1 ]; } catch (Exception $e) { // エラーならロールバック if ( $is_table_lock ) $dbh->rollback(); // 戻り値 $result = [ 'status' => 'error', 'message' => $e->getMessage() ]; } return $result; } // 複数のコメントをINSERTする function insert_comment_multiple( $comment_arr, $is_table_lock ){ try { $dbh = db_open(); if ( $is_table_lock ) { // テーブルロックを実行 $sql = 'LOCK TABLES tbl_comments WRITE, tbl_relations WRITE, tbl_tags WRITE'; $db->query($sql); // 当関数でロックしたので single の方ではロックしない $is_table_lock2 = false; } // ループ結果 $inserted_arr = []; // single の結果 $error_index_arr = []; // エラー番号 // ループで single を実行してINSERTする foreach( $comment_arr as $index => $comment ){ $inserted = insert_comment_single( $comment, $is_table_lock2 ); // エラー番号をセット if ( $inserted['status'] === 'error' ) $error_index_arr[] = $index; // single の結果をセット $inserted_arr[] = $inserted; } // 当関数でテーブルロックした場合、1件でもエラーのときは throw でロールバック if ( $is_table_lock && !empty($error_index_arr) ) throw new Exception( implode(',',$error_index_arr) . 'でエラー'); // エラーなければコミット if ( $is_table_lock ) $dbh->commit(); // 戻り値 $result = [ 'status' =>'ok', 'inserted_arr' => $inserted_arr ]; // 当関数でテーブルロックしない場合、エラー番号を入れる if ( !$is_table_lock && !empty($error_index_arr) ) $result['error_index_arr'] = $error_index_arr; } catch (Exception $e) { // エラーならロールバック if ( $is_table_lock ) $dbh->rollback(); // 戻り値 $result = [ 'status' => 'error', 'inserted_arr' => $inserted_arr, 'message' => $e->getMessage() ]; } return $result; }
Output for git.master, git.master_jit, rfc.property-hooks

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
150.59 ms | 405 KiB | 5 Q