{"id":838,"date":"2025-08-05T16:26:06","date_gmt":"2025-08-05T07:26:06","guid":{"rendered":"https:\/\/nexive.tech\/?p=838"},"modified":"2025-08-05T16:26:06","modified_gmt":"2025-08-05T07:26:06","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-fetchall%e3%81%a8fetch%e3%81%ae%e9%81%95%e3%81%84%e3%81%a8%e4%bd%bf%e3%81%84%e5%88%86%e3%81%91","status":"publish","type":"post","link":"https:\/\/nexive.tech\/?p=838","title":{"rendered":"\u3010\u30b5\u30af\u30c3\u3068\u89e3\u8aac\u3011PHP fetchAll\u3068fetch\u306e\u9055\u3044\u3068\u4f7f\u3044\u5206\u3051"},"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\u304a\u3051\u308b<code>fetch()<\/code>\u3068<code>fetchAll()<\/code>\u306e\u9055\u3044\u3092\u5b9f\u4f8b\u4ed8\u304d\u3067\u89e3\u8aac<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u4ef6\u6570\u3084\u30e1\u30e2\u30ea\u52b9\u7387\u306a\u3069\u3001\u4f7f\u3044\u5206\u3051\u306e\u5224\u65ad\u57fa\u6e96\u3092\u63d0\u793a<\/li>\n\n\n\n<li>\u5b9f\u52d9\u3067\u306e\u9078\u5b9a\u30df\u30b9\u306b\u3088\u308b\u5f71\u97ff\u3084\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u9762\u306e\u6ce8\u610f\u70b9\u3082\u7d39\u4ecb<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. PHP\u306efetchAll\u3068fetch\u3068\u306f\uff1f<\/h2>\n\n\n\n<p>PHP\u306e<code>PDO<\/code>\u30af\u30e9\u30b9\u306b\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e<code>fetch()<\/code>\u3068<code>fetchAll()<\/code>\u3068\u3044\u30462\u3064\u306e\u4ee3\u8868\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3044\u305a\u308c\u3082SQL\u5b9f\u884c\u5f8c\u306b\u7d50\u679c\u30bb\u30c3\u30c8\uff08ResultSet\uff09\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u308f\u308c\u307e\u3059\u304c\u3001\u53d6\u5f97\u65b9\u6cd5\u3068\u30c7\u30fc\u30bf\u91cf\u306b\u5927\u304d\u306a\u9055\u3044\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p><code>fetch()<\/code>\u306f1\u884c\u305a\u3064\u9806\u306b\u53d6\u5f97\u3059\u308b\u306e\u306b\u5bfe\u3057\u3001<code>fetchAll()<\/code>\u306f\u5168\u884c\u3092\u4e00\u62ec\u3067\u53d6\u5f97\u3057\u307e\u3059\u3002\u3053\u306e\u9055\u3044\u306b\u3088\u308a\u3001\u5b9f\u884c\u6642\u306e\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u3084\u51e6\u7406\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3001\u305d\u3057\u3066\u9069\u7528\u3067\u304d\u308b\u5834\u9762\u306b\u5dee\u304c\u751f\u307e\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5b9f\u52d9\u3067\u306f\u3001\u7d50\u679c\u4ef6\u6570\u304c\u6570\u4e07\u4ef6\u4ee5\u4e0a\u306b\u306a\u308b\u5834\u5408\u3084\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u51e6\u7406\u304c\u5fc5\u8981\u306a\u5834\u5408\u306f<code>fetch()<\/code>\u304c\u9069\u3057\u3066\u304a\u308a\u3001\u6570\u4ef6\u301c\u6570\u5341\u4ef6\u7a0b\u5ea6\u306e\u56fa\u5b9a\u30b5\u30a4\u30ba\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u3067\u306f<code>fetchAll()<\/code>\u306e\u65b9\u304c\u6271\u3044\u3084\u3059\u3044\u3053\u3068\u304c\u591a\u3044\u3067\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\">\u5358\u4e00\u884c\u305a\u3064\u51e6\u7406\u3057\u305f\u3044\u3068\u304d\u306e<code>fetch()<\/code><\/h3>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30921\u4ef6\u305a\u3064\u51e6\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f<code>fetch()<\/code>\u3092\u4f7f\u7528\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>&lt;?php\n$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');\n$stmt = $pdo->query('SELECT id, name FROM users');\n\nwhile ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {\n    \/\/ 1\u884c\u305a\u3064\u8868\u793a\u3059\u308b\n    echo $row&#91;'id'&#93; . \": \" . $row&#91;'name'&#93; . \"\\n\";\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\">$stmt<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">query<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;<\/span><span style=\"color: #569CD6\">SELECT<\/span><span style=\"color: #CE9178\"> id, <\/span><span style=\"color: #569CD6\">name<\/span><span style=\"color: #CE9178\"> <\/span><span style=\"color: #569CD6\">FROM<\/span><span style=\"color: #CE9178\"> users&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$row<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$stmt<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">fetch<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">PDO<\/span><span style=\"color: #D4D4D4\">::FETCH_ASSOC)) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ 1\u884c\u305a\u3064\u8868\u793a\u3059\u308b<\/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\">$row<\/span><span style=\"color: #D4D4D4\">&#91;<\/span><span style=\"color: #CE9178\">&#39;id&#39;<\/span><span style=\"color: #D4D4D4\">&#93; <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;: &quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$row<\/span><span style=\"color: #D4D4D4\">&#91;<\/span><span style=\"color: #CE9178\">&#39;name&#39;<\/span><span style=\"color: #D4D4D4\">&#93; <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/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>\u51fa\u529b\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1: \u5c71\u7530\n2: \u4f50\u85e4\n3: \u9234\u6728\n<\/code><\/pre>\n\n\n\n<p>\u3053\u306e\u65b9\u6cd5\u306f\u3001\u884c\u6570\u304c\u591a\u3044\u5834\u5408\u3067\u3082\u30e1\u30e2\u30ea\u52b9\u7387\u304c\u826f\u304f\u3001\u9010\u6b21\u51e6\u7406\u306b\u3082\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u62ec\u53d6\u5f97\u3067\u5168\u4f53\u3092\u6271\u3044\u305f\u3044\u3068\u304d\u306e<code>fetchAll()<\/code><\/h3>\n\n\n\n<p>\u5168\u4ef6\u3092\u914d\u5217\u3067\u4e00\u62ec\u53d6\u5f97\u3057\u305f\u3044\u3068\u304d\u306b\u306f<code>fetchAll()<\/code>\u3092\u4f7f\u3044\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>&lt;?php\n$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');\n$stmt = $pdo->query('SELECT id, name FROM users');\n$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);\n\nforeach ($rows as $row) {\n    echo $row&#91;'id'&#93; . \": \" . $row&#91;'name'&#93; . \"\\n\";\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\">$stmt<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">query<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;<\/span><span style=\"color: #569CD6\">SELECT<\/span><span style=\"color: #CE9178\"> id, <\/span><span style=\"color: #569CD6\">name<\/span><span style=\"color: #CE9178\"> <\/span><span style=\"color: #569CD6\">FROM<\/span><span style=\"color: #CE9178\"> users&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$rows<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$stmt<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">fetchAll<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">PDO<\/span><span style=\"color: #D4D4D4\">::FETCH_ASSOC);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">foreach<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$rows<\/span><span style=\"color: #D4D4D4\"> as <\/span><span style=\"color: #9CDCFE\">$row<\/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: #9CDCFE\">$row<\/span><span style=\"color: #D4D4D4\">&#91;<\/span><span style=\"color: #CE9178\">&#39;id&#39;<\/span><span style=\"color: #D4D4D4\">&#93; <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;: &quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$row<\/span><span style=\"color: #D4D4D4\">&#91;<\/span><span style=\"color: #CE9178\">&#39;name&#39;<\/span><span style=\"color: #D4D4D4\">&#93; <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/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>\u51fa\u529b\u306f<code>fetch()<\/code>\u3068\u540c\u69d8\u3067\u3059\u304c\u3001\u53d6\u5f97\u6642\u70b9\u3067\u5168\u30c7\u30fc\u30bf\u3092\u914d\u5217\u306b\u4fdd\u6301\u3059\u308b\u305f\u3081\u3001\u5927\u91cf\u30c7\u30fc\u30bf\u3067\u306f\u30e1\u30e2\u30ea\u3092\u5727\u8feb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7d50\u679c\u306e\u4ef6\u6570\u3067\u9069\u5207\u306b\u4f7f\u3044\u5206\u3051\u308b<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u306f100\u4e07\u4ef6\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u30b1\u30fc\u30b9\u3067\u3059\uff1a<\/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$stmt = $pdo->query('SELECT * FROM large_table');\n\n\/\/ \u63a8\u5968\u3055\u308c\u306a\u3044\u65b9\u6cd5\uff08fetchAll\uff09\n$allRows = $stmt->fetchAll(); \/\/ \u6570GB\u306e\u30e1\u30e2\u30ea\u6d88\u8cbb\u306e\u53ef\u80fd\u6027\n\n\/\/ \u63a8\u5968\u3055\u308c\u308b\u65b9\u6cd5\uff08fetch\uff09\nwhile ($row = $stmt->fetch()) {\n    \/\/ \u5404\u884c\u3092\u9806\u306b\u51e6\u7406\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\">$stmt<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$pdo<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">query<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;<\/span><span style=\"color: #569CD6\">SELECT<\/span><span style=\"color: #CE9178\"> <\/span><span style=\"color: #D4D4D4\">*<\/span><span style=\"color: #CE9178\"> <\/span><span style=\"color: #569CD6\">FROM<\/span><span style=\"color: #CE9178\"> large_table&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u63a8\u5968\u3055\u308c\u306a\u3044\u65b9\u6cd5\uff08fetchAll\uff09<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">$allRows<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$stmt<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">fetchAll<\/span><span style=\"color: #D4D4D4\">(); <\/span><span style=\"color: #6A9955\">\/\/ \u6570GB\u306e\u30e1\u30e2\u30ea\u6d88\u8cbb\u306e\u53ef\u80fd\u6027<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u63a8\u5968\u3055\u308c\u308b\u65b9\u6cd5\uff08fetch\uff09<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #9CDCFE\">$row<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$stmt<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">fetch<\/span><span style=\"color: #D4D4D4\">()) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u5404\u884c\u3092\u9806\u306b\u51e6\u7406<\/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\u3088\u3046\u306b\u3001\u884c\u6570\u304c\u975e\u5e38\u306b\u591a\u3044\u5834\u5408\u306f<code>fetch()<\/code>\u3092\u4f7f\u3063\u3066\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u51e6\u7406\u3092\u884c\u3046\u306e\u304c\u57fa\u672c\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">fetch vs fetchAll\u306e\u6bd4\u8f03\u3068\u9078\u5b9a\u57fa\u6e96<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9805\u76ee<\/th><th>fetch()<\/th><th>fetchAll()<\/th><\/tr><\/thead><tbody><tr><td>\u51e6\u7406\u65b9\u5f0f<\/td><td>1\u884c\u305a\u3064<\/td><td>\u5168\u884c\u307e\u3068\u3081\u3066<\/td><\/tr><tr><td>\u30e1\u30e2\u30ea\u4f7f\u7528<\/td><td>\u5c11\u306a\u3044<\/td><td>\u591a\u3044\uff08\u4ef6\u6570\u4f9d\u5b58\uff09<\/td><\/tr><tr><td>\u30b3\u30fc\u30c9\u306e\u7c21\u6f54\u3055<\/td><td>\u3084\u3084\u5197\u9577<\/td><td>\u7c21\u6f54<\/td><\/tr><tr><td>\u4ef6\u6570\u304c\u591a\u3044\u5834\u5408<\/td><td>\u25cb<\/td><td>\u00d7<\/td><\/tr><tr><td>\u5c0f\u898f\u6a21\u306a\u7d50\u679c\u30bb\u30c3\u30c8<\/td><td>\u25cb<\/td><td>\u25cb<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5b9f\u52d9\u4e0a\u306f\u3001\u4ef6\u6570\u30fb\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30fb\u51e6\u7406\u306e\u69cb\u9020\uff08\u9010\u6b21\u51e6\u7406\u304b\u4e00\u62ec\u51e6\u7406\u304b\uff09\u306b\u5fdc\u3058\u3066\u9078\u5b9a\u3057\u307e\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<p><code>fetchAll()<\/code>\u306f\u7c21\u6f54\u3067\u6271\u3044\u3084\u3059\u3044\u305f\u3081\u3001\u521d\u5fc3\u8005\u306b\u597d\u307e\u308c\u304c\u3061\u3067\u3059\u304c\u3001\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u4e00\u62ec\u53d6\u5f97\u3057\u3066\u3057\u307e\u3046\u3068PHP\u306e\u30e1\u30e2\u30ea\u4e0a\u9650\uff08\u30c7\u30d5\u30a9\u30eb\u30c8128MB\u7a0b\u5ea6\uff09\u3092\u7c21\u5358\u306b\u8d85\u3048\u3066\u3057\u307e\u3046\u30ea\u30b9\u30af\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001<code>fetch()<\/code>\u3092\u4f7f\u3063\u3066\u3044\u308b\u3064\u3082\u308a\u3067\u3082\u3001<code>fetchAll()<\/code>\u3067\u65e2\u306b\u7d50\u679c\u30bb\u30c3\u30c8\u3092\u6d88\u8cbb\u3057\u3066\u3044\u305f\u5834\u5408\u3001<code>fetch()<\/code>\u3067\u306f\u4f55\u3082\u53d6\u5f97\u3067\u304d\u307e\u305b\u3093\u3002PDO\u306e\u30b9\u30c6\u30fc\u30c8\u30d5\u30eb\u306a\u6027\u8cea\u3092\u7406\u89e3\u3057\u3001\u3069\u306e\u6bb5\u968e\u3067\u3069\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u304b\u3092\u610f\u8b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3055\u3089\u306b\u3001<code>fetch()<\/code>\u3092\u30eb\u30fc\u30d7\u5185\u3067\u4f7f\u7528\u3059\u308b\u969b\u306b\u3001\u60f3\u5b9a\u3088\u308a\u5c11\u306a\u3044\u4ef6\u6570\u3057\u304b\u51e6\u7406\u3055\u308c\u306a\u3044\u5834\u5408\u306f\u3001SQL\u306eLIMIT\u3084\u6761\u4ef6\u53e5\u306b\u3088\u308b\u5236\u9650\u306e\u898b\u843d\u3068\u3057\u3082\u7591\u3046\u3079\u304d\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u307e\u3068\u3081<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fetch()<\/code>\u306f1\u884c\u305a\u3064\u51e6\u7406\u3059\u308b\u5834\u9762\u3001<code>fetchAll()<\/code>\u306f\u5168\u4f53\u3092\u307e\u3068\u3081\u3066\u6271\u3046\u5834\u9762\u3067\u4f7f\u3044\u5206\u3051\u308b<\/li>\n\n\n\n<li>\u5927\u91cf\u4ef6\u6570\u3067\u306f<code>fetch()<\/code>\u304c\u5b89\u5168\u3067\u9ad8\u52b9\u7387\u3001<code>fetchAll()<\/code>\u306f\u30e1\u30e2\u30ea\u4f7f\u7528\u306b\u6ce8\u610f\u304c\u5fc5\u8981<\/li>\n\n\n\n<li>\u5b9f\u52d9\u3067\u306f\u4ef6\u6570\u898f\u6a21\u30fb\u51e6\u7406\u69cb\u9020\u30fb\u30e1\u30e2\u30ea\u5236\u9650\u3092\u8003\u616e\u3057\u305f\u9078\u5b9a\u304c\u91cd\u8981<\/li>\n\n\n\n<li>1\u3064\u306e\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3067\u4e21\u65b9\u3092\u6df7\u5728\u4f7f\u7528\u3057\u306a\u3044\u3088\u3046\u6ce8\u610f<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8 2. PHP\u306efetchAll\u3068fetch\u3068\u306f\uff1f PHP\u306ePDO\u30af\u30e9\u30b9\u306b\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306efetch()\u3068fetchAll()\u3068\u3044\u30462\u3064\u306e\u4ee3\u8868\u7684\u306a\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a\u307e\u3059\u3002 \u3044\u305a\u308c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":916,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-838","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\/838","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=838"}],"version-history":[{"count":2,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/838\/revisions"}],"predecessor-version":[{"id":917,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/838\/revisions\/917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/media\/916"}],"wp:attachment":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}