{"id":175301,"date":"2024-11-08T04:43:20","date_gmt":"2024-11-07T20:43:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/175301\/"},"modified":"2024-11-08T04:43:21","modified_gmt":"2024-11-07T20:43:21","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8php%e5%9c%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e9%8c%84%e5%85%a5%e5%85%a7%e5%ae%b9%e4%b8%a6%e8%87%aa%e5%8b%95%e7%94%9f%e6%88%90%e5%ba%8f%e8%99%9f-php%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/175301\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528PHP\u5728\u6578\u64da\u5eab\u4e2d\u9304\u5165\u5167\u5bb9\u4e26\u81ea\u52d5\u751f\u6210\u5e8f\u865f (php\u6578\u64da\u5eab\u9304\u5165\u5167\u5bb9\u5e8f\u865f)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8php%e5%9c%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e9%8c%84%e5%85%a5%e5%85%a7%e5%ae%b9%e4%b8%a6%e8%87%aa%e5%8b%95%e7%94%9f%e6%88%90%e5%ba%8f%e8%99%9f-QtNusYgBGP\">\u5982\u4f55\u4f7f\u7528PHP\u5728\u6578\u64da\u5eab\u4e2d\u9304\u5165\u5167\u5bb9\u4e26\u81ea\u52d5\u751f\u6210\u5e8f\u865f<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002\u7121\u8ad6\u662f\u7528\u65bc\u5b58\u5132\u7528\u6236\u4fe1\u606f\u3001\u7522\u54c1\u8cc7\u6599\u9084\u662f\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\uff0c\u6578\u64da\u5eab\u90fd\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u7d44\u4ef6\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528PHP\u5728\u6578\u64da\u5eab\u4e2d\u9304\u5165\u5167\u5bb9\u4e26\u81ea\u52d5\u751f\u6210\u5e8f\u865f\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-QtNusYgBGP\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u958b\u767c\u74b0\u5883\u5df2\u7d93\u8a2d\u7f6e\u597d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u8981\u6c42\uff1a<\/p>\n<ul>\n<li>\u5b89\u88ddPHP\uff08\u5efa\u8b70\u4f7f\u7528PHP 7.0\u6216\u66f4\u9ad8\u7248\u672c\uff09<\/li>\n<li>\u5b89\u88ddMySQL\u6216MariaDB\u4f5c\u70ba\u6578\u64da\u5eab<\/li>\n<li>\u4e00\u500bWeb\u4f3a\u670d\u5668\uff08\u5982Apache\u6216Nginx\uff09<\/li>\n<\/ul>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a8%ad%e8%a8%88-QtNusYgBGP\">\u6578\u64da\u5eab\u8a2d\u8a08<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u8a2d\u8a08\u4e00\u500b\u6578\u64da\u5eab\u8868\u4f86\u5b58\u5132\u6211\u5011\u7684\u6578\u64da\u3002\u5047\u8a2d\u6211\u5011\u8981\u9304\u5165\u4e00\u500b\u7522\u54c1\u7684\u540d\u7a31\u548c\u63cf\u8ff0\uff0c\u4e26\u81ea\u52d5\u751f\u6210\u4e00\u500b\u552f\u4e00\u7684\u5e8f\u865f\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u6578\u64da\u5eab\u8868\u7684SQL\u8a9e\u53e5\uff1a<\/p>\n<pre><code>CREATE TABLE products (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(255) NOT NULL,\n    description TEXT NOT NULL\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u8868\u4e2d\uff0c`id`\u5b57\u6bb5\u5c07\u81ea\u52d5\u751f\u6210\u5e8f\u865f\uff0c\u9019\u662f\u901a\u904e`AUTO_INCREMENT`\u5c6c\u6027\u5be6\u73fe\u7684\u3002<\/p>\n<h2 id=\"php%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-QtNusYgBGP\">PHP\u4ee3\u78bc\u793a\u4f8b<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u7de8\u5bebPHP\u4ee3\u78bc\u4f86\u63d2\u5165\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u6578\u64da\u5eab\u9023\u63a5\u53c3\u6578\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"your_database_name\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u6e96\u5099\u63d2\u5165\u6578\u64da\n$name = \"\u7522\u54c1\u540d\u7a31\";\n$description = \"\u7522\u54c1\u63cf\u8ff0\";\n\n$sql = \"INSERT INTO products (name, description) VALUES ('$name', '$description')\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u65b0\u8a18\u9304\u63d2\u5165\u6210\u529f\uff0c\u5e8f\u865f\u70ba: \" . $conn-&gt;insert_id;\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n}\n\n\/\/ \u95dc\u9589\u9023\u63a5\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5efa\u7acb\u4e86\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\uff0c\u7136\u5f8c\u6e96\u5099\u63d2\u5165\u4e00\u689d\u65b0\u8a18\u9304\u3002\u7576\u63d2\u5165\u6210\u529f\u5f8c\uff0c\u6211\u5011\u4f7f\u7528`$conn-&gt;insert_id`\u4f86\u7372\u53d6\u81ea\u52d5\u751f\u6210\u7684\u5e8f\u865f\u3002<\/p>\n<h2 id=\"%e8%99%95%e7%90%86%e7%94%a8%e6%88%b6%e8%bc%b8%e5%85%a5-QtNusYgBGP\">\u8655\u7406\u7528\u6236\u8f38\u5165<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8655\u7406\u7528\u6236\u8f38\u5165\u3002\u70ba\u4e86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\uff0c\u5efa\u8b70\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u6578\u64da\u5eab\u9023\u63a5\u53c3\u6578\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"your_database_name\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u6e96\u5099\u63d2\u5165\u6578\u64da\n$stmt = $conn-&gt;prepare(\"INSERT INTO products (name, description) VALUES (?, ?)\");\n$stmt-&gt;bind_param(\"ss\", $name, $description);\n\n\/\/ \u5047\u8a2d\u9019\u4e9b\u6578\u64da\u4f86\u81ea\u7528\u6236\u8f38\u5165\n$name = $_POST['name'];\n$description = $_POST['description'];\n\nif ($stmt-&gt;execute()) {\n    echo \"\u65b0\u8a18\u9304\u63d2\u5165\u6210\u529f\uff0c\u5e8f\u865f\u70ba: \" . $stmt-&gt;insert_id;\n} else {\n    echo \"\u932f\u8aa4: \" . $stmt-&gt;error;\n}\n\n\/\/ \u95dc\u9589\u9023\u63a5\n$stmt-&gt;close();\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u4f7f\u7528\u4e86\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u5b89\u5168\u5730\u63d2\u5165\u7528\u6236\u8f38\u5165\u7684\u6578\u64da\uff0c\u5f9e\u800c\u63d0\u9ad8\u4e86\u61c9\u7528\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-QtNusYgBGP\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u4f7f\u7528PHP\u5728\u6578\u64da\u5eab\u4e2d\u9304\u5165\u5167\u5bb9\u4e26\u81ea\u52d5\u751f\u6210\u5e8f\u865f\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u7ba1\u7406\u7684\u6548\u7387\uff0c\u9084\u589e\u5f37\u4e86\u61c9\u7528\u7684\u5b89\u5168\u6027\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u6280\u8853\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528PHP\u5728\u6578\u64da\u5eab\u4e2d\u9304\u5165\u5167\u5bb9\u4e26\u81ea\u52d5\u751f\u6210\u5e8f\u865f\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\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-175301","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175301","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=175301"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175301\/revisions"}],"predecessor-version":[{"id":175302,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175301\/revisions\/175302"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=175301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=175301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=175301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}