{"id":836,"date":"2025-08-05T16:24:53","date_gmt":"2025-08-05T07:24:53","guid":{"rendered":"https:\/\/nexive.tech\/?p=836"},"modified":"2025-08-05T16:24:54","modified_gmt":"2025-08-05T07:24:54","slug":"%e3%80%90%e3%82%b5%e3%82%af%e3%83%83%e3%81%a8%e8%a7%a3%e8%aa%ac%e3%80%91php-%e3%83%88%e3%83%a9%e3%83%b3%e3%82%b6%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e5%87%a6%e7%90%86%e3%81%a8%e4%be%8b%e5%a4%96","status":"publish","type":"post","link":"https:\/\/nexive.tech\/?p=836","title":{"rendered":"\u3010\u30b5\u30af\u30c3\u3068\u89e3\u8aac\u3011PHP \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u3068\u4f8b\u5916\u5bfe\u5fdc"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP\u306ePDO\u306b\u3088\u308b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5236\u5fa1\u306e\u57fa\u672c\u3092\u89e3\u8aac<\/li>\n\n\n\n<li>try-catch\u3092\u4f7f\u3063\u305f\u4f8b\u5916\u51e6\u7406\u3068\u306e\u7d44\u307f\u5408\u308f\u305b\u65b9\u3092\u7d39\u4ecb<\/li>\n\n\n\n<li>\u5b9f\u52d9\u3067\u91cd\u8981\u306a\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3068\u30b3\u30df\u30c3\u30c8\u306e\u8a2d\u8a08\u5224\u65ad\u306b\u3082\u89e6\u308c\u308b<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. PHP\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u3068\u4f8b\u5916\u5bfe\u5fdc\u3068\u306f\uff1f<\/h2>\n\n\n\n<p>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u3068\u306f\u3001\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u300c\u3072\u3068\u307e\u3068\u307e\u308a\u306e\u51e6\u7406\u5358\u4f4d\u300d\u3068\u3057\u3066\u6271\u3044\u3001\u5168\u4f53\u304c\u6210\u529f\u3057\u305f\u3068\u304d\u3060\u3051\u53cd\u6620\u3057\u3001\u3069\u308c\u304b\u304c\u5931\u6557\u3057\u305f\u5834\u5408\u306b\u306f\u3059\u3079\u3066\u3092\u53d6\u308a\u6d88\u3059\u4ed5\u7d44\u307f\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u4fdd\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>PHP\u3067\u306f\u4e3b\u306bPDO\uff08PHP Data Objects\uff09\u3092\u4f7f\u3063\u3066\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u6271\u3044\u307e\u3059\u3002\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u306e\u958b\u59cb\u3001\u30b3\u30df\u30c3\u30c8\u3001\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u660e\u793a\u7684\u306b\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u5236\u5fa1\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5b9f\u52d9\u3067\u306f\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u4f8b\u5916\u51e6\u7406\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u4f7f\u3046\u306e\u304c\u4e00\u822c\u7684\u3067\u3059\u3002\u30c7\u30fc\u30bf\u66f4\u65b0\u4e2d\u306b\u30a8\u30e9\u30fc\u304c\u8d77\u304d\u305f\u5834\u5408\u306f\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u3001\u6b63\u5e38\u6642\u306f\u30b3\u30df\u30c3\u30c8\u3057\u307e\u3059\u3002\u3053\u306e\u4e00\u9023\u306e\u6d41\u308c\u3092try-catch\u30d6\u30ed\u30c3\u30af\u5185\u3067\u51e6\u7406\u3059\u308b\u3053\u3068\u3067\u3001\u5b89\u5168\u306a\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u8a73\u7d30\u89e3\u8aac<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5178\u578b\u7684\u306a\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u4f7f\u3044\u65b9<\/h3>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u8907\u6570\u5b9f\u884c\u3057\u3001\u9014\u4e2d\u3067\u5931\u6557\u3057\u305f\u5834\u5408\u306b\u3059\u3079\u3066\u3092\u53d6\u308a\u6d88\u3059\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>&lt;?php\n$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');\n$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\ntry {\n    \/\/ \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u958b\u59cb\n    $pdo->beginTransaction();\n\n    \/\/ \u30c7\u30fc\u30bf\u306e\u633f\u5165\u51e6\u7406\n    $pdo->exec(\"INSERT INTO users (name) VALUES ('Taro')\");\n    $pdo->exec(\"INSERT INTO orders (user_id, amount) VALUES (LAST_INSERT_ID(), 1000)\");\n\n    \/\/ \u6b63\u5e38\u306b\u5b8c\u4e86\u3057\u305f\u5834\u5408\u306f\u30b3\u30df\u30c3\u30c8\n    $pdo->commit();\n} catch (Exception $e) {\n    \/\/ \u4f8b\u5916\u767a\u751f\u6642\u306f\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\n    $pdo->rollBack();\n    echo \"\u30a8\u30e9\u30fc: \" . $e->getMessage();\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">&lt;?php<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #569CD6\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">PDO<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;mysql:host=localhost;dbname=testdb&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;user&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;pass&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">setAttribute<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">PDO<\/span><span style=\"color: #D4D4D4\">::ATTR_ERRMODE, <\/span><span style=\"color: #4EC9B0\">PDO<\/span><span style=\"color: #D4D4D4\">::ERRMODE_EXCEPTION);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">try<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u958b\u59cb<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">beginTransaction<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u30c7\u30fc\u30bf\u306e\u633f\u5165\u51e6\u7406<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">exec<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #569CD6\">INSERT INTO<\/span><span style=\"color: #CE9178\"> users (<\/span><span style=\"color: #569CD6\">name<\/span><span style=\"color: #CE9178\">) <\/span><span style=\"color: #569CD6\">VALUES<\/span><span style=\"color: #CE9178\"> (&#39;Taro&#39;)&quot;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">exec<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #569CD6\">INSERT INTO<\/span><span style=\"color: #CE9178\"> orders (user_id, amount) <\/span><span style=\"color: #569CD6\">VALUES<\/span><span style=\"color: #CE9178\"> (LAST_INSERT_ID(), <\/span><span style=\"color: #B5CEA8\">1000<\/span><span style=\"color: #CE9178\">)&quot;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u6b63\u5e38\u306b\u5b8c\u4e86\u3057\u305f\u5834\u5408\u306f\u30b3\u30df\u30c3\u30c8<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">commit<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u4f8b\u5916\u767a\u751f\u6642\u306f\u30ed\u30fc\u30eb\u30d0\u30c3\u30af<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">rollBack<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;\u30a8\u30e9\u30fc: &quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">getMessage<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001<code>users<\/code> \u3068 <code>orders<\/code> \u306e\u4e21\u65b9\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3057\u3066\u51e6\u7406\u3092\u884c\u3044\u3001\u3069\u3061\u3089\u304b\u304c\u5931\u6557\u3059\u308b\u3068\u3059\u3079\u3066\u3092\u5143\u306b\u623b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u5fd8\u308c\u306e\u4f8b\u3068\u305d\u306e\u5bfe\u7b56<\/h3>\n\n\n\n<p>\u4f8b\u5916\u304c\u8d77\u304d\u305f\u969b\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u3057\u306a\u3044\u30b3\u30fc\u30c9\u306f\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u672a\u5b8c\u4e86\u306e\u307e\u307e\u6b8b\u3063\u3066\u3057\u307e\u3044\u3001\u610f\u56f3\u3057\u306a\u3044\u52d5\u4f5c\u3092\u5f15\u304d\u8d77\u3053\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>try {\n    $pdo->beginTransaction();\n    \/\/ ... \u4f55\u3089\u304b\u306e\u64cd\u4f5c\n    throw new Exception(\"\u51e6\u7406\u4e2d\u306b\u30a8\u30e9\u30fc\u767a\u751f\");\n    $pdo->commit();\n} catch (Exception $e) {\n    \/\/ \u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u306a\u3044 \u2192 \u554f\u984c\n    echo $e->getMessage();\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">try<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">beginTransaction<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ ... \u4f55\u3089\u304b\u306e\u64cd\u4f5c<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">throw<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;\u51e6\u7406\u4e2d\u306b\u30a8\u30e9\u30fc\u767a\u751f&quot;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">commit<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u306a\u3044 \u2192 \u554f\u984c<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">getMessage<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>\u6539\u5584\u70b9\uff1a<\/strong> catch\u5185\u3067\u5fc5\u305a <code>rollBack()<\/code> \u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u660e\u793a\u7684\u306a\u518d\u30b9\u30ed\u30fc\u306b\u3088\u308b\u4f8b\u5916\u306e\u4f1d\u64ad<\/h3>\n\n\n\n<p>\u4e0a\u4f4d\u5c64\u3067\u4f8b\u5916\u51e6\u7406\u3092\u7d71\u4e00\u3057\u305f\u3044\u5834\u5408\u3001catch\u5f8c\u306b\u4f8b\u5916\u3092\u518d\u30b9\u30ed\u30fc\u3059\u308b\u3053\u3068\u3067\u5bfe\u5fdc\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>try {\n    $pdo->beginTransaction();\n    \/\/ \u8907\u6570\u306eDB\u64cd\u4f5c\n    $pdo->commit();\n} catch (Exception $e) {\n    $pdo->rollBack();\n    throw $e; \/\/ \u547c\u3073\u51fa\u3057\u5143\u306b\u518d\u30b9\u30ed\u30fc\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">try<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">beginTransaction<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u8907\u6570\u306eDB\u64cd\u4f5c<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">commit<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">rollBack<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">throw<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ \u547c\u3073\u51fa\u3057\u5143\u306b\u518d\u30b9\u30ed\u30fc<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u4f8b\u5916\u306e\u30ed\u30b0\u3092\u5225\u9014\u7ba1\u7406\u3057\u305f\u3044\u5834\u5408\u3084\u3001\u547c\u3073\u51fa\u3057\u5074\u3067\u753b\u9762\u8868\u793a\u3092\u5236\u5fa1\u3057\u305f\u3044\u5834\u5408\u306b\u6709\u52b9\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u3088\u304f\u3042\u308b\u30df\u30b9\u30fb\u8aa4\u89e3\u30fb\u843d\u3068\u3057\u7a74<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>PDO\u306e\u30a8\u30e9\u30fc\u30e2\u30fc\u30c9\u8a2d\u5b9a\u3092\u5fd8\u308c\u308b\u3068\u4f8b\u5916\u304c\u767a\u751f\u3057\u306a\u3044<\/strong>\uff1a<code>PDO::ATTR_ERRMODE<\/code> \u3092 <code>PDO::ERRMODE_EXCEPTION<\/code> \u306b\u3057\u3066\u304a\u304b\u306a\u3044\u3068\u3001\u5931\u6557\u6642\u306b\u4f8b\u5916\u304c\u767a\u751f\u305b\u305a <code>false<\/code> \u304c\u8fd4\u308b\u3060\u3051\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/strong>\uff1aPDO\u306f\u30cd\u30a4\u30c6\u30a3\u30d6\u306b\u30cd\u30b9\u30c8\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u305b\u3093\u3002\u30cd\u30b9\u30c8\u306e\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u5074\u3067\u5de5\u592b\u304c\u5fc5\u8981\u3067\u3059\u3002<\/li>\n\n\n\n<li><strong>\u4f8b\u5916\u3092\u63e1\u308a\u3064\u3076\u3057\u3066\u3057\u307e\u3046<\/strong>\uff1acatch\u5185\u3067\u30a8\u30e9\u30fc\u30ed\u30b0\u3060\u3051\u51fa\u529b\u3057\u3066 <code>throw<\/code> \u3057\u306a\u3044\u3068\u3001\u547c\u3073\u51fa\u3057\u5143\u3067\u306f\u6b63\u5e38\u7d42\u4e86\u3068\u8a8d\u8b58\u3055\u308c\u308b\u305f\u3081\u6ce8\u610f\u3002<\/li>\n\n\n\n<li><strong>\u30b3\u30df\u30c3\u30c8\u5fd8\u308c<\/strong>\uff1a\u660e\u793a\u7684\u306b <code>commit()<\/code> \u3092\u66f8\u304b\u306a\u3044\u3068\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306f\u78ba\u5b9a\u3055\u308c\u307e\u305b\u3093\u3002\u660e\u793a\u7684\u306a\u7d42\u4e86\u51e6\u7406\u304c\u5fc5\u8981\u3067\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u307e\u3068\u3081<\/h2>\n\n\n\n<p>PHP\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u306f\u3001PDO\u3068try-catch\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u5b89\u5168\u306b\u5236\u5fa1\u3067\u304d\u307e\u3059\u3002\u91cd\u8981\u306a\u306e\u306f\u3001\u51e6\u7406\u6210\u529f\u6642\u306e <code>commit()<\/code> \u3068\u5931\u6557\u6642\u306e <code>rollBack()<\/code> \u3092\u9069\u5207\u306b\u914d\u7f6e\u3057\u3001\u4f8b\u5916\u767a\u751f\u6642\u306b\u78ba\u5b9f\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3059\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001PDO\u306e\u30a8\u30e9\u30fc\u30e2\u30fc\u30c9\u8a2d\u5b9a\u3092\u5fd8\u308c\u306a\u3044\u3053\u3068\u3001catch\u5f8c\u306e\u518d\u30b9\u30ed\u30fc\u306b\u3088\u308b\u4f8b\u5916\u4f1d\u64ad\u306a\u3069\u3082\u3001\u5b9f\u52d9\u3067\u306f\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8 2. PHP\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u3068\u4f8b\u5916\u5bfe\u5fdc\u3068\u306f\uff1f \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u3068\u306f\u3001\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u300c\u3072\u3068\u307e\u3068\u307e\u308a\u306e\u51e6\u7406\u5358\u4f4d\u300d\u3068\u3057\u3066\u6271\u3044\u3001\u5168\u4f53\u304c\u6210\u529f\u3057\u305f\u3068\u304d\u3060\u3051\u53cd\u6620\u3057\u3001\u3069\u308c\u304b\u304c\u5931\u6557\u3057\u305f\u5834\u5408\u306b\u306f\u3059 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":914,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=836"}],"version-history":[{"count":2,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/836\/revisions"}],"predecessor-version":[{"id":915,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/836\/revisions\/915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/media\/914"}],"wp:attachment":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}