{"id":76348,"date":"2024-10-16T05:55:16","date_gmt":"2024-10-15T21:55:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76348\/"},"modified":"2024-10-16T05:55:16","modified_gmt":"2024-10-15T21:55:16","slug":"mysql%e6%95%b8%e6%93%9a%e8%ae%80%e5%8f%96%ef%bc%8cextjs%e5%92%8cphp-json","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76348\/","title":{"rendered":"MySQL\u6578\u64da\u8b80\u53d6\uff0cExtJS\u548cPHP JSON"},"content":{"rendered":"<h1 id=\"mysql%e6%95%b8%e6%93%9a%e8%ae%80%e5%8f%96%ef%bc%8cextjs%e5%92%8cphp-json-VRdXGHxRNT\">MySQL\u6578\u64da\u8b80\u53d6\uff0cExtJS\u548cPHP JSON<\/h1>\n<p>\u5728\u73fe\u4ee3\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u6578\u64da\u7684\u8b80\u53d6\u548c\u986f\u793a\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002MySQL\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u7d93\u5e38\u88ab\u7528\u4f86\u5b58\u5132\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5f0f\u7684\u6578\u64da\u3002\u800cExtJS\u4f5c\u70ba\u4e00\u500b\u5f37\u5927\u7684JavaScript\u6846\u67b6\uff0c\u5247\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u7528\u6236\u754c\u9762\u7d44\u4ef6\uff0c\u80fd\u5920\u9ad8\u6548\u5730\u5c55\u793a\u9019\u4e9b\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u8b80\u53d6\uff0c\u4e26\u901a\u904ePHP\u5c07\u6578\u64da\u8f49\u63db\u70baJSON\u683c\u5f0f\uff0c\u4ee5\u4fbfExtJS\u9032\u884c\u8655\u7406\u548c\u986f\u793a\u3002<\/p>\n<h2 id=\"mysql%e6%95%b8%e6%93%9a%e8%ae%80%e5%8f%96-VRdXGHxRNT\">MySQL\u6578\u64da\u8b80\u53d6<\/h2>\n<p>MySQL\u6578\u64da\u5eab\u7684\u6578\u64da\u8b80\u53d6\u901a\u5e38\u4f7f\u7528SQL\u67e5\u8a62\u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9e\u540d\u70ba\u201cusers\u201d\u7684\u8868\u4e2d\u8b80\u53d6\u6578\u64da\uff1a<\/p>\n<pre><code>SELECT * FROM users;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u8fd4\u56de\u201cusers\u201d\u8868\u4e2d\u7684\u6240\u6709\u8a18\u9304\u3002\u70ba\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u901a\u5e38\u6703\u6839\u64da\u9700\u8981\u9078\u64c7\u7279\u5b9a\u7684\u5217\u6216\u4f7f\u7528\u689d\u4ef6\u904e\u6ffe\u6578\u64da\uff1a<\/p>\n<pre><code>SELECT id, name, email FROM users WHERE active = 1;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u50c5\u8fd4\u56de\u6d3b\u8e8d\u7528\u6236\u7684ID\u3001\u59d3\u540d\u548c\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8php%e8%ae%80%e5%8f%96mysql%e6%95%b8%e6%93%9a-VRdXGHxRNT\">\u4f7f\u7528PHP\u8b80\u53d6MySQL\u6578\u64da<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528PDO\u6216MySQLi\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\u4e26\u57f7\u884c\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528PDO\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$dsn = 'mysql:host=localhost;dbname=testdb';\n$username = 'root';\n$password = 'password';\n\ntry {\n    $pdo = new PDO($dsn, $username, $password);\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    $stmt = $pdo-&gt;query('SELECT id, name, email FROM users WHERE active = 1');\n    $users = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);\n\n    echo json_encode($users);\n} catch (PDOException $e) {\n    echo 'Connection failed: ' . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5efa\u7acb\u4e86\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\uff0c\u7136\u5f8c\u57f7\u884c\u67e5\u8a62\u4ee5\u7372\u53d6\u6d3b\u8e8d\u7528\u6236\u7684\u6578\u64da\u3002\u6700\u5f8c\uff0c\u6211\u5011\u4f7f\u7528`json_encode`\u51fd\u6578\u5c07\u6578\u64da\u8f49\u63db\u70baJSON\u683c\u5f0f\u3002<\/p>\n<h2 id=\"extjs%e8%88%87json%e6%95%b8%e6%93%9a-VRdXGHxRNT\">ExtJS\u8207JSON\u6578\u64da<\/h2>\n<p>ExtJS\u662f\u4e00\u500b\u5f37\u5927\u7684JavaScript\u6846\u67b6\uff0c\u5c08\u9580\u7528\u65bc\u69cb\u5efa\u8c50\u5bcc\u7684\u7528\u6236\u754c\u9762\u3002\u5b83\u80fd\u5920\u8f15\u9b06\u5730\u8655\u7406JSON\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684ExtJS\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9ePHP\u7372\u53d6JSON\u6578\u64da\u4e26\u986f\u793a\u5728\u7db2\u9801\u4e0a\uff1a<\/p>\n<pre><code>Ext.onReady(function() {\n    Ext.Ajax.request({\n        url: 'get_users.php',\n        method: 'GET',\n        success: function(response) {\n            var data = Ext.decode(response.responseText);\n            var store = Ext.create('Ext.data.Store', {\n                fields: ['id', 'name', 'email'],\n                data: data\n            });\n\n            Ext.create('Ext.grid.Panel', {\n                title: 'Active Users',\n                store: store,\n                columns: [\n                    { text: 'ID', dataIndex: 'id' },\n                    { text: 'Name', dataIndex: 'name' },\n                    { text: 'Email', dataIndex: 'email' }\n                ],\n                height: 400,\n                width: 600,\n                renderTo: Ext.getBody()\n            });\n        }\n    });\n});\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528`Ext.Ajax.request`\u65b9\u6cd5\u5f9ePHP\u7372\u53d6JSON\u6578\u64da\uff0c\u7136\u5f8c\u5c07\u5176\u52a0\u8f09\u5230ExtJS\u7684\u6578\u64da\u5b58\u5132\u4e2d\uff0c\u4e26\u5728\u7db2\u9801\u4e0a\u986f\u793a\u70ba\u4e00\u500b\u8868\u683c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VRdXGHxRNT\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u8b80\u53d6\uff0c\u4e26\u5229\u7528PHP\u5c07\u6578\u64da\u8f49\u63db\u70baJSON\u683c\u5f0f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u6578\u64da\u96c6\u6210\u5230ExtJS\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u7684\u53ef\u7528\u6027\uff0c\u9084\u589e\u5f37\u4e86\u7528\u6236\u754c\u9762\u7684\u4e92\u52d5\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u9078\u64c7\u5408\u9069\u7684\u4f3a\u670d\u5668\u74b0\u5883\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u5c0d\u65bc<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u6216<code><a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a><\/code>\u6709\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to efficiently read MySQL data using ExtJS and PHP JSON for seamless web application development.<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-76348","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=76348"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76348\/revisions"}],"predecessor-version":[{"id":76349,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76348\/revisions\/76349"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}