{"id":431,"date":"2016-12-20T01:25:59","date_gmt":"2016-12-19T16:25:59","guid":{"rendered":"http:\/\/okamurax.com\/?p=431"},"modified":"2016-12-23T10:04:38","modified_gmt":"2016-12-23T01:04:38","slug":"php-fetch%e3%83%86%e3%82%b9%e3%83%88","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=431","title":{"rendered":"PHP Fetch\u30c6\u30b9\u30c8"},"content":{"rendered":"<p>&nbsp;<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\n\r\n\/*\r\ncreate database test_db character set utf8;\r\nuse test_db\r\ncreate table test_tb (id int, name varchar(8))default character set=utf8;\r\n\r\ninsert into test_tb (id,name) values (1,'taro');\r\ninsert into test_tb (id,name) values (1,'hanako');\r\ninsert into test_tb (id,name) values (1,'hanako');\r\ninsert into test_tb (id,name) values (1,'ziro');\r\ninsert into test_tb (id,name) values (1,'ziro');\r\n*\/\r\n\r\n$db = new PDO(\"mysql:host=localhost;dbname=test_db;charset=utf8\",\"id\",\"pass\");\r\n$stm = $db-&gt;query(\"select count(*) from test_tb\");\r\nvar_dump($stm-&gt;fetchColumn()); \/\/1\u30bb\u30eb\u306e\u6307\u5b9a\u306a\u306e\u3067\u6587\u5b57\u5217\u3067\u8fd4\u3063\u3066\u304f\u308b\u3002\r\n\r\n\/*\r\n\u4e0a\u8a18\u306fselect\u306e\u7d50\u679c\u884c\u6570\u3092\u53d6\u5f97\u3059\u308b\u5b9a\u756a\u3002\r\n\r\nfetchColumn\r\n\u6b21\u306e1\u884c\u53d6\u5f97\u3059\u308b\u3002\u5217\u756a\u53f7(0\u304b\u3089)\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3002\r\n*\/\r\n\r\n$db = new PDO(\"mysql:host=localhost;dbname=test_db;charset=utf8\",\"id\",\"pass\");\r\n$stm = $db-&gt;query(\"select * from test_tb\");\r\n$cells = array();\r\n$cells = $stm-&gt;fetchAll();\/\/\u30de\u30c8\u30ea\u30af\u30b9\u3067\u8fd4\u3063\u3066\u304f\u308b\u3002\r\necho $cells[4][1]; \/\/[\u884c][\u5217] 0\u30b9\u30bf\u30fc\u30c8\r\n\/\/ -&gt; ziro\r\n\r\necho \"\\n---------------------------------------------------------------------\\n\";\r\n\r\n$db = new PDO(\"mysql:host=localhost;dbname=test_db;charset=utf8\",\"id\",\"pass\");\r\nforeach($db-&gt;query(\"select * from test_tb\") as $row){\r\n\techo $row[0], \"|\", $row[1], \"\\n\";\r\n\t\/\/\u4f8b\u3048\u3070\u3053\u3053\u3067exit\u3068\u3059\u308b\u3068\u30011\u884c\u76ee\u306e\u53d6\u5f97\u3067\u7d42\u4e86\u3057\u3066\u3057\u307e\u3046\u305f\u3081\u30011\u884c\u306e\u307f\u8868\u793a\u3055\u308c\u308b\u3002\r\n\t}\r\n\r\n\/*\r\n$query\u3092foreach\u3067\u56de\u3059\u3068$row\u306b1\u884c\u305a\u3064\u914d\u5217\u3067\u53d6\u5f97\u3067\u304d\u308b\u6a21\u69d8\u3002\r\n*\/\r\n\r\necho \"\\n---------------------------------------------------------------------\\n\";\r\n\r\n$db = new PDO(\"mysql:host=localhost;dbname=test_db;charset=utf8\",\"id\",\"pass\");\r\n$rows = array(); \/\/\u3053\u308c\u304c\u306a\u3044\u3068\u8b66\u544a\u3002\r\nforeach($db-&gt;query(\"select * from test_tb\") as $row) array_push($rows, $row);\r\n\r\n\/\/fetchAll\u3068\u540c\u3058\u3088\u3046\u306a\u53d6\u5f97\u306b\u306a\u308a\u305d\u3046\u3002\r\n\r\necho $rows[4][1]; \/\/[\u884c][\u5217] 0\u30b9\u30bf\u30fc\u30c8\r\n\/\/ -&gt; ziro\r\n\r\necho count($rows); \/\/ -&gt; 5\r\n\r\n\/\/\u884c\u306e\u30eb\u30fc\u30d7\u3067count\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3001\u6dfb\u3048\u5b57\u306f0\u30b9\u30bf\u30fc\u30c8\u3067count\u306f\u91cf\u306a\u306e\u3067\u6ce8\u610f\uff01\r\n\r\necho \"\\n---------------------------------------------------------------------\\n\";\r\n\r\n$db = new PDO(\"mysql:host=localhost;dbname=test_db;charset=utf8\",\"id\",\"pass\");\r\n$stm = $db-&gt;query(\"select * from test_tb\");\r\nvar_dump($stm-&gt;fetch()); \/\/1\u884c\u305a\u3064\u53d6\u5f97\u3059\u308b\u3002\r\n\r\necho \"\\n---------------------------------------------------------------------\\n\";\r\n\r\n$db = new PDO(\"mysql:host=localhost;dbname=test_db;charset=utf8\",\"id\",\"pass\");\r\n$stm = $db-&gt;query(\"select * from test_tb\");\r\nvar_dump($stm-&gt;fetchColumn()); \/\/1\u884c\u305a\u3064(1\u5217\u306e\u307f)\u53d6\u5f97\u3059\u308b\u3002\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &lt;?php \/* create database test_db character set utf8; use test_db create table test_tb (id int, name  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=431\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;PHP Fetch\u30c6\u30b9\u30c8&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3],"class_list":["post-431","post","type-post","status-publish","format-standard","hentry","category-1","tag-php"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=431"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions\/432"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}