{"id":96668,"date":"2024-10-20T11:22:16","date_gmt":"2024-10-20T03:22:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96668\/"},"modified":"2024-10-20T11:22:17","modified_gmt":"2024-10-20T03:22:17","slug":"php%e4%b8%ad%e6%89%b9%e9%87%8f%e5%88%aa%e9%99%a4mysql%e4%b8%ad%e7%9b%b8%e5%90%8c%e5%89%8d%e7%b6%b4%e7%9a%84%e6%95%b8%e6%93%9a%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96668\/","title":{"rendered":"php\u4e2d\u6279\u91cf\u522a\u9664Mysql\u4e2d\u76f8\u540c\u524d\u7db4\u7684\u6578\u64da\u8868"},"content":{"rendered":"<h1 id=\"php%e4%b8%ad%e6%89%b9%e9%87%8f%e5%88%aa%e9%99%a4mysql%e4%b8%ad%e7%9b%b8%e5%90%8c%e5%89%8d%e7%b6%b4%e7%9a%84%e6%95%b8%e6%93%9a%e8%a1%a8-CaQVhFzIxm\">PHP\u4e2d\u6279\u91cf\u522a\u9664MySQL\u4e2d\u76f8\u540c\u524d\u7db4\u7684\u6578\u64da\u8868<\/h1>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u662f\u4e00\u9805\u91cd\u8981\u7684\u4efb\u52d9\u3002\u7279\u5225\u662f\u5728\u4f7f\u7528MySQL\u4f5c\u70ba\u6578\u64da\u5eab\u6642\uff0c\u958b\u767c\u8005\u7d93\u5e38\u9700\u8981\u9032\u884c\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\u64cd\u4f5c\u3002\u7576\u9700\u8981\u6279\u91cf\u522a\u9664\u5177\u6709\u76f8\u540c\u524d\u7db4\u7684\u6578\u64da\u8868\u6642\uff0c\u4f7f\u7528PHP\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6548\u7387\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528PHP\u6279\u91cf\u522a\u9664MySQL\u4e2d\u76f8\u540c\u524d\u7db4\u7684\u6578\u64da\u8868\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e7%90%86%e8%a7%a3mysql%e6%95%b8%e6%93%9a%e8%a1%a8%e7%9a%84%e7%b5%90%e6%a7%8b-CaQVhFzIxm\">\u7406\u89e3MySQL\u6578\u64da\u8868\u7684\u7d50\u69cb<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u4e86\u89e3MySQL\u6578\u64da\u8868\u7684\u7d50\u69cb\u3002\u6bcf\u500b\u6578\u64da\u8868\u90fd\u6709\u4e00\u500b\u552f\u4e00\u7684\u540d\u7a31\uff0c\u901a\u5e38\u7531\u5b57\u6bcd\u3001\u6578\u5b57\u548c\u4e0b\u5283\u7dda\u7d44\u6210\u3002\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u4f7f\u7528\u76f8\u540c\u7684\u524d\u7db4\u4f86\u547d\u540d\u591a\u500b\u6578\u64da\u8868\uff0c\u4ee5\u4fbf\u65bc\u7ba1\u7406\u548c\u8b58\u5225\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4ee5\u4e0b\u6578\u64da\u8868\uff1a<\/p>\n<ul>\n<li>user_data_2021<\/li>\n<li>user_data_2022<\/li>\n<li>user_data_2023<\/li>\n<li>order_data_2021<\/li>\n<li>order_data_2022<\/li>\n<\/ul>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u4ee5 <code>user_data_<\/code> \u958b\u982d\u7684\u6578\u64da\u8868\u90fd\u53ef\u4ee5\u88ab\u6279\u91cf\u522a\u9664\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8php%e6%89%b9%e9%87%8f%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a%e8%a1%a8-CaQVhFzIxm\">\u4f7f\u7528PHP\u6279\u91cf\u522a\u9664\u6578\u64da\u8868<\/h2>\n<p>\u8981\u6279\u91cf\u522a\u9664\u5177\u6709\u76f8\u540c\u524d\u7db4\u7684\u6578\u64da\u8868\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528PHP\u7684PDO\uff08PHP Data Objects\uff09\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u57f7\u884c\u76f8\u61c9\u7684SQL\u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u5177\u9ad4\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab-CaQVhFzIxm\">1. \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab<\/h3>\n<pre><code>\n$dsn = 'mysql:host=localhost;dbname=your_database';\n$username = 'your_username';\n$password = 'your_password';\n\ntry {\n    $pdo = new PDO($dsn, $username, $password);\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n} catch (PDOException $e) {\n    echo 'Connection failed: ' . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<h3 id=\"2-%e7%8d%b2%e5%8f%96%e6%89%80%e6%9c%89%e6%95%b8%e6%93%9a%e8%a1%a8%e5%90%8d%e7%a8%b1-CaQVhFzIxm\">2. \u7372\u53d6\u6240\u6709\u6578\u64da\u8868\u540d\u7a31<\/h3>\n<pre><code>\n$prefix = 'user_data_';\n$query = \"SHOW TABLES LIKE :prefix\";\n$stmt = $pdo-&gt;prepare($query);\n$stmt-&gt;execute(['prefix' =&gt; $prefix . '%']);\n$tables = $stmt-&gt;fetchAll(PDO::FETCH_COLUMN);\n<\/code><\/pre>\n<h3 id=\"3-%e6%89%b9%e9%87%8f%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a%e8%a1%a8-CaQVhFzIxm\">3. \u6279\u91cf\u522a\u9664\u6578\u64da\u8868<\/h3>\n<pre><code>\nforeach ($tables as $table) {\n    $dropQuery = \"DROP TABLE IF EXISTS `$table`\";\n    $pdo-&gt;exec($dropQuery);\n}\necho \"\u6210\u529f\u522a\u9664\u6240\u6709\u4ee5 $prefix \u70ba\u524d\u7db4\u7684\u6578\u64da\u8868\u3002\";\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-CaQVhFzIxm\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u57f7\u884c\u6279\u91cf\u522a\u9664\u64cd\u4f5c\u4e4b\u524d\uff0c\u958b\u767c\u8005\u61c9\u8a72\u7279\u5225\u5c0f\u5fc3\uff0c\u56e0\u70ba\u9019\u4e00\u64cd\u4f5c\u662f\u4e0d\u53ef\u9006\u7684\u3002\u5efa\u8b70\u5728\u522a\u9664\u4e4b\u524d\u5099\u4efd\u6578\u64da\u5eab\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002\u6b64\u5916\uff0c\u78ba\u4fdd\u5728\u57f7\u884c\u522a\u9664\u64cd\u4f5c\u6642\uff0c\u6c92\u6709\u5176\u4ed6\u9032\u7a0b\u6b63\u5728\u4f7f\u7528\u9019\u4e9b\u6578\u64da\u8868\uff0c\u4ee5\u907f\u514d\u6f5b\u5728\u7684\u6578\u64da\u640d\u58de\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CaQVhFzIxm\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u4f7f\u7528PHP\u6279\u91cf\u522a\u9664MySQL\u4e2d\u76f8\u540c\u524d\u7db4\u7684\u6578\u64da\u8868\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u5de5\u4f5c\u6548\u7387\uff0c\u9084\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u6578\u64da\u5eab\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u7ba1\u7406\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u670d\u52d9\u53ef\u4ee5\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\uff0c\u78ba\u4fdd\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6d41\u66a2\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728PHP\u4e2d\u6279\u91cf\u522a\u9664MySQL\u4e2d\u5177\u6709\u76f8\u540c\u524d\u7db4\u7684\u6578\u64da\u8868\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/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-96668","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96668","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=96668"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96668\/revisions"}],"predecessor-version":[{"id":96669,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96668\/revisions\/96669"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}