{"id":96420,"date":"2024-10-20T09:16:17","date_gmt":"2024-10-20T01:16:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96420\/"},"modified":"2024-10-20T09:16:18","modified_gmt":"2024-10-20T01:16:18","slug":"php-mysql%e5%89%b5%e5%bb%ba%e8%87%a8%e6%99%82%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96420\/","title":{"rendered":"php mysql\u5275\u5efa\u81e8\u6642\u8868"},"content":{"rendered":"<h1 id=\"php-mysql%e5%89%b5%e5%bb%ba%e8%87%a8%e6%99%82%e8%a1%a8-qvDGcWiEbi\">PHP MySQL\u5275\u5efa\u81e8\u6642\u8868<\/h1>\n<p>\u5728Web\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002MySQL\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u7d93\u5e38\u8207PHP\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u4fbf\u65bc\u6578\u64da\u7684\u5b58\u5132\u548c\u6aa2\u7d22\u3002\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u9700\u8981\u5275\u5efa\u81e8\u6642\u8868\u4f86\u8655\u7406\u7279\u5b9a\u7684\u6578\u64da\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728PHP\u4e2d\u4f7f\u7528MySQL\u5275\u5efa\u81e8\u6642\u8868\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e8%87%a8%e6%99%82%e8%a1%a8%ef%bc%9f-qvDGcWiEbi\">\u4ec0\u9ebc\u662f\u81e8\u6642\u8868\uff1f<\/h2>\n<p>\u81e8\u6642\u8868\u662fMySQL\u4e2d\u4e00\u7a2e\u7279\u6b8a\u7684\u8868\uff0c\u5b83\u7684\u751f\u547d\u9031\u671f\u50c5\u9650\u65bc\u7576\u524d\u7684\u6578\u64da\u5eab\u9023\u63a5\u3002\u7576\u9023\u63a5\u95dc\u9589\u6642\uff0c\u81e8\u6642\u8868\u6703\u81ea\u52d5\u88ab\u522a\u9664\u3002\u9019\u4f7f\u5f97\u81e8\u6642\u8868\u975e\u5e38\u9069\u5408\u7528\u65bc\u5b58\u5132\u4e2d\u9593\u7d50\u679c\u6216\u9032\u884c\u81e8\u6642\u8a08\u7b97\uff0c\u800c\u4e0d\u6703\u5f71\u97ff\u5230\u4e3b\u6578\u64da\u5eab\u7684\u7d50\u69cb\u548c\u6578\u64da\u3002<\/p>\n<h2 id=\"%e5%89%b5%e5%bb%ba%e8%87%a8%e6%99%82%e8%a1%a8%e7%9a%84%e8%aa%9e%e6%b3%95-qvDGcWiEbi\">\u5275\u5efa\u81e8\u6642\u8868\u7684\u8a9e\u6cd5<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u5275\u5efa\u81e8\u6642\u8868\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE TEMPORARY TABLE table_name (\n    column1 datatype,\n    column2 datatype,\n    ...\n);<\/code><\/pre>\n<p>\u9019\u88e1\u7684 <code>table_name<\/code> \u662f\u81e8\u6642\u8868\u7684\u540d\u7a31\uff0c<code>column1<\/code> \u548c <code>column2<\/code> \u662f\u8868\u4e2d\u7684\u5217\u540d\uff0c<code>datatype<\/code> \u5247\u662f\u76f8\u61c9\u7684\u6578\u64da\u985e\u578b\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8php%e5%89%b5%e5%bb%ba%e8%87%a8%e6%99%82%e8%a1%a8%e7%9a%84%e7%a4%ba%e4%be%8b-qvDGcWiEbi\">\u4f7f\u7528PHP\u5275\u5efa\u81e8\u6642\u8868\u7684\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528PHP\u548cMySQL\u5275\u5efa\u81e8\u6642\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database_name\";\n\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u5275\u5efa\u81e8\u6642\u8868\n$sql = \"CREATE TEMPORARY TABLE temp_table (\n    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(30) NOT NULL,\n    age INT(3) NOT NULL\n)\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u81e8\u6642\u8868\u5275\u5efa\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"<br>\" . $conn-&gt;error;\n}\n\n\/\/ \u95dc\u9589\u9023\u63a5\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u4f7f\u7528 <code>CREATE TEMPORARY TABLE<\/code> \u8a9e\u53e5\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba <code>temp_table<\/code> \u7684\u81e8\u6642\u8868\u3002\u8a72\u8868\u5305\u542b\u4e09\u500b\u5217\uff1a<code>id<\/code>\u3001<code>name<\/code> \u548c <code>age<\/code>\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e8%87%a8%e6%99%82%e8%a1%a8%e7%9a%84%e5%84%aa%e5%8b%a2-qvDGcWiEbi\">\u4f7f\u7528\u81e8\u6642\u8868\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u81e8\u6642\u8868\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u4e3b\u6578\u64da\u5eab\u7684\u67e5\u8a62\u8ca0\u64d4\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u6578\u64da\u9694\u96e2\uff1a<\/strong>\u81e8\u6642\u8868\u7684\u6578\u64da\u4e0d\u6703\u5f71\u97ff\u5230\u4e3b\u6578\u64da\u5eab\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u81ea\u7531\u5730\u9032\u884c\u6e2c\u8a66\u548c\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u7c21\u5316\u67e5\u8a62\uff1a<\/strong>\u5728\u8907\u96dc\u67e5\u8a62\u4e2d\uff0c\u4f7f\u7528\u81e8\u6642\u8868\u53ef\u4ee5\u4f7f\u67e5\u8a62\u908f\u8f2f\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-qvDGcWiEbi\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528\u81e8\u6642\u8868\u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u81e8\u6642\u8868\u7684\u540d\u7a31\u5fc5\u9808\u662f\u552f\u4e00\u7684\uff0c\u5426\u5247\u6703\u5c0e\u81f4\u932f\u8aa4\u3002<\/li>\n<li>\u81e8\u6642\u8868\u53ea\u80fd\u5728\u5275\u5efa\u5b83\u7684\u9023\u63a5\u4e2d\u8a2a\u554f\uff0c\u5176\u4ed6\u9023\u63a5\u7121\u6cd5\u8a2a\u554f\u3002<\/li>\n<li>\u5728\u4f7f\u7528\u5b8c\u81e8\u6642\u8868\u5f8c\uff0c\u61c9\u53ca\u6642\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\uff0c\u4ee5\u91cb\u653e\u8cc7\u6e90\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-qvDGcWiEbi\">\u7e3d\u7d50<\/h2>\n<p>\u81e8\u6642\u8868\u5728PHP\u548cMySQL\u7684\u958b\u767c\u4e2d\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u6709\u6548\u5730\u8655\u7406\u6578\u64da\u3002\u901a\u904e\u5275\u5efa\u81e8\u6642\u8868\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728\u4e0d\u5f71\u97ff\u4e3b\u6578\u64da\u5eab\u7684\u60c5\u6cc1\u4e0b\u9032\u884c\u6578\u64da\u64cd\u4f5c\u548c\u8a08\u7b97\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u4f7f\u7528\u81e8\u6642\u8868\u7121\u7591\u662f\u4e00\u500b\u660e\u667a\u7684\u9078\u64c7\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728PHP\u4e2d\u4f7f\u7528MySQL\u5275\u5efa\u81e8\u6642\u8868\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u67e5\u8a62\u64cd\u4f5c\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-96420","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96420","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=96420"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96420\/revisions"}],"predecessor-version":[{"id":96421,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96420\/revisions\/96421"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}