{"id":25016,"date":"2024-11-21T10:26:19","date_gmt":"2024-11-21T02:26:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25016\/"},"modified":"2024-11-21T10:26:19","modified_gmt":"2024-11-21T02:26:19","slug":"swoole%e5%ae%9e%e6%88%98%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25016\/","title":{"rendered":"Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169940472775434.jpg\" class=\"aligncenter\" title=\"Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u63d2\u56fe\" alt=\"Swoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u63d2\u56fe\" \/><\/p>\n<p>Swoole \u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/p>\n<h4>\u5f15\u8a00<\/h4>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u5927\u91cf\u7684\u6570\u636e\u9700\u8981\u5b58\u50a8\u548c\u5904\u7406\u3002\u5bf9\u4e8e\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u4f20\u7edf\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u5f0f\u4f1a\u9762\u4e34\u963b\u585e\u3001\u6027\u80fd\u74f6\u9888\u7b49\u95ee\u9898\uff0c\u800c\u534f\u7a0b\u5219\u6210\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u4e00\u79cd\u6709\u6548\u65b9\u5f0f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<h4>\u4ec0\u4e48\u662f Swoole<\/h4>\n<p>Swoole \u662f\u4e00\u4e2a\u57fa\u4e8e PHP \u8bed\u8a00\u5f00\u53d1\u7684\u534f\u7a0b\u7f51\u7edc\u6846\u67b6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u5e76\u53d1\u7f16\u7a0b\u3002\u901a\u8fc7 Swoole\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u6027\u80fd\u3002<\/p>\n<h4>Swoole \u534f\u7a0b\u4e0e\u4f20\u7edf\u65b9\u5f0f\u7684\u5bf9\u6bd4<\/h4>\n<p>\u4f20\u7edf\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u5f0f\u901a\u5e38\u662f\u901a\u8fc7\u963b\u585e I\/O \u6765\u5b9e\u73b0\u7684\u3002\u5f53\u4e00\u4e2a\u64cd\u4f5c\u6267\u884c\u65f6\uff0c\u5176\u4ed6\u64cd\u4f5c\u5fc5\u987b\u7b49\u5f85\u5f53\u524d\u64cd\u4f5c\u5b8c\u6210\u540e\u624d\u80fd\u8fdb\u884c\u3002\u8fd9\u6837\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u4f1a\u9020\u6210\u5927\u91cf\u7684\u7ebf\u7a0b\u963b\u585e\uff0c\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u548c\u8d44\u6e90\u6d88\u8017\u589e\u52a0\u3002<\/p>\n<p>Swoole \u534f\u7a0b\u5219\u91c7\u7528\u975e\u963b\u585e I\/O \u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5360\u7528\u7684\u5185\u5b58\u8d44\u6e90\u76f8\u5bf9\u8f83\u5c11\uff0c\u80fd\u591f\u66f4\u597d\u5730\u4f18\u5316\u5e76\u53d1\u4efb\u52a1\u7684\u5904\u7406\u3002<\/p>\n<h4>\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6b65\u9aa4<\/h4>\n<p>\u4e0b\u9762\u5c06\u4ecb\u7ecd\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u5177\u4f53\u6b65\u9aa4\uff0c\u4ee5 MySQL \u6570\u636e\u5e93\u4e3a\u4f8b\u3002<\/p>\n<h5>\u6b65\u9aa4\u4e00\uff1a\u5b89\u88c5 Swoole \u548c MySQL \u6269\u5c55<\/h5>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5 Swoole \u6269\u5c55\u548c MySQL \u6269\u5c55\u3002\u53ef\u4ee5\u4f7f\u7528 pecl \u5de5\u5177\u8fdb\u884c\u5b89\u88c5\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u7f16\u8bd1\u5b89\u88c5\u3002<\/p>\n<h5>\u6b65\u9aa4\u4e8c\uff1a\u521d\u59cb\u5316 Swoole \u534f\u7a0b\u73af\u5883<\/h5>\n<p>\u5728\u4ee3\u7801\u7684\u6700\u5f00\u59cb\uff0c\u6211\u4eec\u9700\u8981\u5148\u521d\u59cb\u5316 Swoole \u534f\u7a0b\u73af\u5883\u3002\u53ef\u4ee5\u4f7f\u7528 Coun() \u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<pre>Coun(function() {\n    \/\/ Your code here\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h5>\u6b65\u9aa4\u4e09\uff1a\u521b\u5efa MySQL \u8fde\u63a5<\/h5>\n<p>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa MySQL \u8fde\u63a5\u3002\u53ef\u4ee5\u4f7f\u7528 SwooleCoroutineMySQL \u7c7b\u6765\u521b\u5efa\u8fde\u63a5\u3002<\/p>\n<pre>$db = new SwooleCoroutineMySQL();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u521b\u5efa\u8fde\u63a5\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u8fde\u63a5\u53c2\u6570\uff0c\u4f8b\u5982\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u7b49\u3002<\/p>\n<pre>$db-&amp;gt;connect([\n    'host' =&amp;gt; 'localhost',\n    'port' =&amp;gt; 3306,\n    'user' =&amp;gt; 'root',\n    'password' =&amp;gt; 'password',\n    'database' =&amp;gt; 'test',\n]);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h5>\u6b65\u9aa4\u56db\uff1a\u6267\u884c SQL \u8bed\u53e5<\/h5>\n<p>\u521b\u5efa\u4e86 MySQL \u8fde\u63a5\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c SQL \u8bed\u53e5\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528 query() \u65b9\u6cd5\u8fdb\u884c\u67e5\u8be2\u64cd\u4f5c\uff0c\u4f7f\u7528 exec() \u65b9\u6cd5\u8fdb\u884c\u63d2\u5165\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<pre>$result = $db-&amp;gt;query('SELECT * FROM `users`');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>$result = $db-&amp;gt;exec('INSERT INTO `users` (`name`, `age`) VALUES (\"John\", 25)');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h5>\u6b65\u9aa4\u4e94\uff1a\u5904\u7406\u7ed3\u679c\u96c6<\/h5>\n<p>\u6267\u884c SQL \u8bed\u53e5\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u7ed3\u679c\u96c6\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528 fetch() \u65b9\u6cd5\u83b7\u53d6\u4e00\u6761\u8bb0\u5f55\uff0c\u4f7f\u7528 fetchAll() \u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u8bb0\u5f55\u3002<\/p>\n<pre>while ($row = $result-&amp;gt;fetch()) {\n    \/\/ Process each row\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>$rows = $result-&amp;gt;fetchAll();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h5>\u6b65\u9aa4\u516d\uff1a\u5173\u95ed MySQL \u8fde\u63a5<\/h5>\n<p>\u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c\u540e\uff0c\u6211\u4eec\u9700\u8981\u5173\u95ed MySQL \u8fde\u63a5\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<pre>$db-&amp;gt;close();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h4>\u603b\u7ed3<\/h4>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u4f7f\u7528 Swoole \u534f\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u6027\u80fd\u3002\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\u5173\u4e8e Swoole \u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u8bf7\u53c2\u8003 Swoole \u5b98\u65b9\u6587\u6863\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u6709\u6240\u5e2e\u52a9\uff0c\u8c22\u8c22\u9605\u8bfb\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swoole \u5b9e\u6218\uff1a\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c \u5f15\u8a00 \u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0c\u5927\u91cf\u7684\u6570\u636e\u9700\u8981\u5b58\u50a8\u548c\u5904\u7406\u3002\u5bf9\u4e8e\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u4f20\u7edf\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u5f0f\u4f1a\u9762\u4e34\u963b\u585e\u3001\u6027\u80fd\u74f6\u9888\u7b49\u95ee\u9898\uff0c\u800c\u534f\u7a0b\u5219\u6210\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u4e00\u79cd\u6709\u6548\u65b9\u5f0f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4ec0\u4e48\u662f Swoole Swoole \u662f\u4e00\u4e2a\u57fa\u4e8e PHP \u8bed\u8a00\u5f00\u53d1\u7684\u534f\u7a0b\u7f51\u7edc\u6846\u67b6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u5e76\u53d1\u7f16\u7a0b\u3002\u901a\u8fc7 Swoole\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u534f\u7a0b\u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u6027\u80fd\u3002 Swoole \u534f\u7a0b\u4e0e\u4f20\u7edf\u65b9\u5f0f\u7684\u5bf9\u6bd4 \u4f20\u7edf\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u65b9\u5f0f\u901a\u5e38\u662f\u901a\u8fc7\u963b\u585e I\/O \u6765\u5b9e\u73b0\u7684\u3002\u5f53\u4e00\u4e2a\u64cd\u4f5c\u6267\u884c\u65f6\uff0c\u5176\u4ed6\u64cd\u4f5c\u5fc5\u987b\u7b49\u5f85\u5f53\u524d\u64cd\u4f5c\u5b8c\u6210\u540e\u624d\u80fd\u8fdb\u884c\u3002\u8fd9\u6837\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u4f1a\u9020\u6210\u5927\u91cf\u7684\u7ebf\u7a0b\u963b\u585e\uff0c\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u548c\u8d44\u6e90\u6d88\u8017\u589e\u52a0\u3002 Swoole \u534f\u7a0b\u5219\u91c7\u7528\u975e\u963b\u585e I\/O \u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5360\u7528\u7684\u5185\u5b58\u8d44\u6e90\u76f8\u5bf9\u8f83\u5c11\uff0c\u80fd\u591f\u66f4\u597d\u5730\u4f18\u5316\u5e76\u53d1\u4efb\u52a1\u7684\u5904\u7406\u3002 \u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6b65\u9aa4 \u4e0b\u9762\u5c06\u4ecb\u7ecd\u4f7f\u7528 Swoole \u534f\u7a0b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u5177\u4f53\u6b65\u9aa4\uff0c\u4ee5 MySQL \u6570\u636e\u5e93\u4e3a\u4f8b\u3002 \u6b65\u9aa4\u4e00\uff1a\u5b89\u88c5 Swoole \u548c MySQL \u6269\u5c55 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5 Swoole \u6269\u5c55\u548c MySQL \u6269\u5c55\u3002\u53ef\u4ee5\u4f7f\u7528 pecl \u5de5\u5177\u8fdb\u884c\u5b89\u88c5\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u7f16\u8bd1\u5b89\u88c5\u3002 \u6b65\u9aa4\u4e8c\uff1a\u521d\u59cb\u5316 Swoole \u534f\u7a0b\u73af\u5883 \u5728\u4ee3\u7801\u7684\u6700\u5f00\u59cb\uff0c\u6211\u4eec\u9700\u8981\u5148\u521d\u59cb\u5316 Swoole \u534f\u7a0b\u73af\u5883\u3002\u53ef\u4ee5\u4f7f\u7528 Coun() \u51fd\u6570\u6765\u5b9e\u73b0\u3002 Coun(function() { \/\/ Your code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-25016","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=25016"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25016\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}