{"id":1142,"date":"2020-08-30T17:04:28","date_gmt":"2020-08-30T08:04:28","guid":{"rendered":"https:\/\/dalomo.net\/blog\/?p=1142"},"modified":"2020-09-12T13:59:33","modified_gmt":"2020-09-12T04:59:33","slug":"%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e3%82%b5%e3%82%a4%e3%83%88%e3%82%92%e4%bd%9c%e3%82%8a%e3%81%9f%e3%81%84%e2%91%a3-php%e3%81%aepdo%e3%81%a7db","status":"publish","type":"post","link":"https:\/\/dalomo.net\/blog\/2020\/08\/30\/1142\/","title":{"rendered":"\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f7f\u3063\u305f\u30b5\u30a4\u30c8\u3092\u4f5c\u308a\u305f\u3044\u2463 PHP\u306ePDO\u3067DB\u306b\u63a5\u7d9a\u3059\u308b"},"content":{"rendered":"<h1>PHP Data Object<\/h1>\n<p>DB\u304b\u3089\u30c7\u30fc\u30bf\u5f15\u3063\u5f35\u308a\u51fa\u305b\u308c\u3070\u3042\u3068\u306f\u306a\u3093\u3068\u304b\u306a\u308b\u3093\u3058\u3083\u306a\u304b\u308d\u3046\u304b\u3068\u3044\u3046\u671f\u5f85\u306e\u5143\u3001\u3068\u306b\u304b\u304f\u307e\u305a\u306fPHP\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3059\u308b\u3084\u308a\u65b9\u3092\u8abf\u3079\u308b\u3002\u305d\u306e\u3046\u3061PDO\u3063\u3066\u3044\u3046\u3084\u3064\u3092\u898b\u3064\u3051\u305f\u3002\u3084\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>DB\u306b\u63a5\u7d9a\u3059\u308b<\/h2>\n<p>\u63a5\u7d9a\u60c5\u5831\u306f\u5225\u306b\u30b5\u30fc\u30d0\u30fc\u5916\u304b\u3089\u898b\u308c\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3063\u307d\u3044\u3093\u3060\u3051\u3069\u3001\u306a\u3093\u304b\u3053\u3046\u3043\u306e\u3067\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u306b\u79fb\u3057\u3068\u304f\u3002<\/p>\n<pre><code class=\"php\">&lt;?php\r\n  $dsn = 'dsn:host=localhost;dbname=shogi';\r\n  $user = '****';\r\n  $password = '******';\r\n?&gt;<\/code><\/pre>\n<p>\u3067shogi_config.php\u306b\u4fdd\u5b58\u3002\u305d\u3093\u3067\u30e1\u30a4\u30f3\u306e\u65b9\u3067require\u3067\u5f15\u3063\u5f35\u308b\u3002\u3067\u3001\u8272\u3005\u66f8\u3044\u3066<\/p>\n<pre><code class=\"php\">&lt;?php\r\n  require ('shogi_config.php');\r\n\r\n  try {\r\n    $pdo = new PDO($dsn, $user, $password);\r\n    print ('\u63a5\u7d9a\u3057\u307e\u3057\u305f\u3002');\r\n  } catch (PDOException $ext) {\r\n    die('\u63a5\u7d9a\u3057\u3066\u306a\u3044\u3002'.$ext-&gt;getMessage());\r\n  }\r\n\r\n  $pdo = null;\r\n?&gt;<\/code><\/pre>\n<p>\u3046\u3080\u3002\u6700\u521drequire\u306e\u5f8c\u306b;\u5165\u308c\u308b\u306e\u5fd8\u308c\u3066\u3066\u3001syntax error, unexpected &#8216;try&#8217; (T_TRY)\u3063\u3066\u3044\u3046\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u304a\u3063\u305f\u3002\u307e\u3041\u305d\u308c\u306f\u305d\u308c\u3068\u3057\u3066\u3002\u3053\u306e\u72b6\u614b\u3067\u30b5\u30fc\u30d0\u30fc\u306b\u30a2\u30c3\u30d7\u3057\u3066\u307f\u305f\u3089\u3001\u63a5\u7d9a\u3057\u307e\u3057\u305f\u3002\u3063\u3066\u8868\u793a\u3055\u308c\u305f\u3002\u5e78\u5148\u304c\u3088\u308d\u3057\u3044\u3002<\/p>\n<h2>SQL\u3092\u767a\u884c\u3059\u308b<\/h2>\n<p>\u3068\u308a\u307e\u5909\u6570\u3068\u304b\u4f7f\u308f\u305a\u306b\u3079\u305f\u66f8\u304d\u3059\u308b\u3002<\/p>\n<pre><code class=\"php\">  $sql = \"SELECT * FROM data WHERE id=1234\";\r\n\r\n  $state = $pdo-&gt;query($sql);\r\n  $res = $state-&gt;fetch(PDO::FETCH_NUM);\r\n  foreach ($res as $r =&gt; $v) {\r\n    print($v.\"\\n\");  \r\n  }<\/code><\/pre>\n<p>\u3066\u66f8\u304f\u3002query\u6295\u3052\u3066\u623b\u308a\u5024\u3092fetch\u3057\u3066print\u3002<\/p>\n<p>\u7d50\u679c<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1143\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi1-300x12.png\" alt=\"\" width=\"300\" height=\"12\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi1-300x12.png 300w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi1-1024x42.png 1024w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi1-768x31.png 768w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi1.png 1366w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u6587\u5b57\u5316\u3051\u3057\u3068\u308b\u306a\u30fc\u3002\u307e\u305f\u6587\u5b57\u30b3\u30fc\u30c9\u304c\u3069\u3046\u305f\u3089\u304b\u306a\u3002config.php\u306e$dsn\u306b<\/p>\n<pre><code class=\"php\">charset=utf8<\/code><\/pre>\n<p>\u3092\u8ffd\u52a0\u3057\u305f\u3089\u76f4\u3063\u305f\uff01\u3058\u3083\u3042\u30ec\u30b3\u30fc\u30c9\u304c\u8907\u6570\u3042\u308b\u5834\u5408\u306f\u3069\u3046\u3059\u3093\u306e\uff1f\u3068\u8a66\u3057\u3066\u307f\u308b\u3002sql\u3092<\/p>\n<pre><code class=\"php\">$sql = \"SELECT * FROM data WHERE id IN (1234, 1235)\";<\/code><\/pre>\n<p>\u3066\u3057\u3066\u307f\u305f\u3051\u3069\u30011234\u306e\u65b9\u3057\u304b\u8868\u793a\u3055\u308c\u306a\u3044\u3002\u3067\u3001\u8272\u3005\u8a66\u3057\u3066\u307f\u305f\u3089<\/p>\n<pre><code class=\"php\">$sql = \"SELECT * FROM data WHERE id IN (1234, 1235)\";\r\n\r\n  $state = $pdo-&gt;query($sql);\r\n  $res = $state-&gt;fetch(PDO::FETCH_ASSOC);\r\n  foreach ($res as $r =&gt; $v) {\r\n    print($v.\"\\n&lt;br&gt;\");  \r\n  }\r\n  \r\n  $res = $state-&gt;fetch(PDO::FETCH_ASSOC);\r\n  foreach ($res as $r =&gt; $v) {\r\n    print($v.\"\\n&lt;br&gt;\");  \r\n  }<\/code><\/pre>\n<p>\u3063\u3064\u3063\u3066\u3001fetch\u30922\u56de\u3084\u3063\u3066\u307f\u305f\u3089\u3069\u3063\u3061\u3082\u8868\u793a\u3055\u308c\u305f\u3002<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1144\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi2-300x154.png\" alt=\"\" width=\"300\" height=\"154\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi2-300x154.png 300w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi2.png 678w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u3046\u30fc\u3093\u3001\u306a\u3093\u304b\u3042\u3093\u307e\u30b3\u30fc\u30c9\u304c\u30ad\u30ec\u30a4\u3058\u3083\u306a\u3044\u306d\u3002\u3069\u3046\u3084\u308b\u306e\u304c\u3044\u3044\u3093\u3060\u308d\u3046\u306a\u3041\u3002\u307e\u3041\u305d\u308c\u306f\u7f6e\u3044\u3068\u3044\u3066\u3001\u5909\u6570\u4f7f\u3046\u3084\u3064\u3084\u308b\u3002<\/p>\n<h3>\u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u30fc<\/h3>\n<p>\u5916\u90e8\u5165\u529b\u3092\u307e\u3093\u307eSQL\u6587\u306b\u3064\u306a\u3044\u3067\u767a\u884c\u3057\u305f\u308a\u3059\u308b\u3068\u3001\u60aa\u610f\u3092\u6301\u3063\u305f\u653b\u6483\u8005\u306b\u60aa\u3044\u3053\u3068\u3055\u308c\u308b\u3002SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3063\u3066\u3084\u3064\u3060\uff01\u57fa\u672c\u60c5\u5831\u3067\u51fa\u3066\u304d\u305f\u306a\u3041\u3002\u3067\u305d\u308c\u3092\u9632\u3050\u305f\u3081\u306b\u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u30fc\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3001\u3068\u3044\u3046\u3053\u3068\u307f\u305f\u3044\u3002SQL\u6587\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u4f5c\u3063\u3066\u304a\u3044\u3066\u3001\u30d7\u30ea\u30da\u30a2\u30fc\u30c9\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4ecb\u3057\u3066\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u307f\u305f\u3044\u306a\u6d41\u308c\u3089\u3057\u3044\u3002\u3053\u306e\u524d\u4f5c\u3063\u305fDB\u3092\u691c\u7d22\u3057\u305f\u3044\u3063\u3066\u306a\u308b\u3068\u3001\u65e5\u4ed8\u3001\u4f5c\u6210\u8005\u3001\u624b\u6570\u3067\u7d5e\u308b\u3088\u3046\u306a\u4f7f\u3044\u65b9\u306b\u306a\u308b\u306e\u304b\u306a\u30fc\u3063\u3066\u601d\u3046\u3002\u305d\u308c\u7528\u306bSQL\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u4f5c\u308c\u308b\u3060\u308d\u304b\u3002<\/p>\n<pre><code class=\"php\">$sql = &lt;&lt;&lt;EOD\r\n          SELECT * FROM data \r\n          WHERE date BETWEEN :date_from AND :date_to\r\n          AND maker = :maker\r\n          AND steps = :steps\r\nEOD;\r\n\r\n  $df = '2020-08-01';\r\n  $dt = '2020-08-15';\r\n  $maker = '';\r\n  $steps = 3;\r\n\r\n  $state = $pdo-&gt;prepare($sql);\r\n  $state-&gt;bindValue(':date_from',$df);\r\n  $state-&gt;bindValue(':date_to',$dt);\r\n  $state-&gt;bindValue(':maker',$maker);\r\n  $state-&gt;bindValue(':steps',$steps);\r\n\r\n  $state-&gt;execute();\r\n  $res = $state-&gt;fetchAll(PDO::FETCH_ASSOC);\r\n\r\n  var_dump($res);<\/code><\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u304b\u306a\u2026\u3002\u3053\u308c\u4f8b\u3048\u3070maker\u306f\u30ef\u30a4\u30eb\u30c9\u30ab\u30fc\u30c9\u3067\u3044\u3044\u3001\u307f\u305f\u3044\u306a\u6642\u3063\u3066\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u3093\u3060\u308d\u3046\u306a\uff5e\u3002<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1146\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi3-300x47.png\" alt=\"\" width=\"300\" height=\"47\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi3-300x47.png 300w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi3-1024x159.png 1024w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi3-768x119.png 768w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/08\/shogi3.png 1366w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3053\u3093\u306a\u3002\u3061\u3083\u3093\u3068\u53d6\u5f97\u3067\u304d\u3066\u308b\u3063\u307d\u3044\u3002\u305d\u3057\u305f\u3089\u3001\u3093\u30fc\u3001\u30d5\u30a9\u30fc\u30e0\u3092\u4f5c\u3063\u3066\u307f\u3088\u3046\u304b\u306a\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP Data Object DB\u304b\u3089\u30c7\u30fc\u30bf\u5f15\u3063\u5f35\u308a\u51fa\u305b\u308c\u3070\u3042\u3068\u306f\u306a\u3093\u3068\u304b\u306a\u308b\u3093\u3058\u3083\u306a\u304b\u308d\u3046\u304b\u3068\u3044\u3046\u671f\u5f85\u306e\u5143\u3001\u3068\u306b\u304b\u304f\u307e\u305a\u306fPHP\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3059\u308b\u3084\u308a\u65b9\u3092\u8abf\u3079\u308b\u3002\u305d\u306e\u3046\u3061PDO\u3063\u3066\u3044\u3046\u3084\u3064\u3092\u898b\u3064\u3051\u305f\u3002\u3084\u3063\u3066\u307f\u307e\u3057 &hellip; <a href=\"https:\/\/dalomo.net\/blog\/2020\/08\/30\/1142\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1144,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[36,122,125],"class_list":["post-1142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-8","tag-mariadb","tag-php","tag-125"],"_links":{"self":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/comments?post=1142"}],"version-history":[{"count":2,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1142\/revisions"}],"predecessor-version":[{"id":1147,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/1142\/revisions\/1147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media\/1144"}],"wp:attachment":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media?parent=1142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/categories?post=1142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/tags?post=1142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}