{"id":195495,"date":"2024-11-13T15:28:23","date_gmt":"2024-11-13T07:28:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/195495\/"},"modified":"2024-11-13T15:28:24","modified_gmt":"2024-11-13T07:28:24","slug":"%e5%8e%9f%e7%94%9fphp%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e7%b0%a1%e5%96%ae%e5%8f%88%e9%ab%98%e6%95%88-%e5%8e%9f%e7%94%9fphp%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/195495\/","title":{"rendered":"\u539f\u751fPHP\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u7c21\u55ae\u53c8\u9ad8\u6548 (\u539f\u751fphp\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%8e%9f%e7%94%9fphp%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e7%b0%a1%e5%96%ae%e5%8f%88%e9%ab%98%e6%95%88-yZTPgKfpkm\">\u539f\u751fPHP\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u7c21\u55ae\u53c8\u9ad8\u6548<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u5df2\u7d93\u6210\u70ba\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002PHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u670d\u52d9\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u5f0f\u4f86\u9023\u63a5\u548c\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528\u539f\u751fPHP\u4f86\u9023\u63a5\u6578\u64da\u5eab\uff0c\u4e26\u9032\u884c\u57fa\u672c\u7684\u6578\u64da\u64cd\u4f5c\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u4e0a\u624b\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u3002<\/p>\n<h2 id=\"php%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%97%9c%e4%bf%82-yZTPgKfpkm\">PHP\u8207\u6578\u64da\u5eab\u7684\u95dc\u4fc2<\/h2>\n<p>PHP\u901a\u5e38\u8207MySQL\u6578\u64da\u5eab\u4e00\u8d77\u4f7f\u7528\uff0c\u9019\u662f\u56e0\u70baMySQL\u662f\u4e00\u500b\u958b\u6e90\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u4e26\u4e14\u8207PHP\u7684\u96c6\u6210\u975e\u5e38\u7dca\u5bc6\u3002\u4f7f\u7528\u539f\u751fPHP\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u4e0d\u50c5\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u7b2c\u4e09\u65b9\u5eab\u7684\u4f9d\u8cf4\uff0c\u9084\u80fd\u63d0\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\u3002<\/p>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-yZTPgKfpkm\">\u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u4f7f\u7528PHP\u9023\u63a5MySQL\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86MySQL\u670d\u52d9\u5668\u3002\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u5efa\u7acb\u9023\u63a5\uff1a<\/p>\n<pre><code>&lt;?php\n$servername = \"localhost\"; \/\/ \u6578\u64da\u5eab\u4f3a\u670d\u5668\u5730\u5740\n$username = \"username\"; \/\/ \u6578\u64da\u5eab\u7528\u6236\u540d\n$password = \"password\"; \/\/ \u6578\u64da\u5eab\u5bc6\u78bc\n$dbname = \"database_name\"; \/\/ \u6578\u64da\u5eab\u540d\u7a31\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}\necho \"\u9023\u63a5\u6210\u529f\";\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86MySQLi\u64f4\u5c55\u4f86\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8acb\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u66ff\u63db\u4f3a\u670d\u5668\u5730\u5740\u3001\u7528\u6236\u540d\u3001\u5bc6\u78bc\u548c\u6578\u64da\u5eab\u540d\u7a31\u3002<\/p>\n<h2 id=\"%e5%9f%b7%e8%a1%8c%e5%9f%ba%e6%9c%ac%e7%9a%84%e6%95%b8%e6%93%9a%e6%93%8d%e4%bd%9c-yZTPgKfpkm\">\u57f7\u884c\u57fa\u672c\u7684\u6578\u64da\u64cd\u4f5c<\/h2>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-yZTPgKfpkm\">\u63d2\u5165\u6578\u64da<\/h3>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u5c31\u53ef\u4ee5\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$sql = \"INSERT INTO users (username, email) VALUES ('testuser', 'test@example.com')\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u65b0\u8a18\u9304\u63d2\u5165\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-yZTPgKfpkm\">\u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u67e5\u8a62\u6578\u64da\u662f\u6700\u5e38\u898b\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$sql = \"SELECT id, username, email FROM users\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6bcf\u884c\u6578\u64da\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"id: \" . $row[\"id\"]. \" - \u7528\u6236\u540d: \" . $row[\"username\"]. \" - \u96fb\u5b50\u90f5\u4ef6: \" . $row[\"email\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-yZTPgKfpkm\">\u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u66f4\u65b0\u6578\u64da\u7684\u793a\u4f8b\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<pre><code>&lt;?php\n$sql = \"UPDATE users SET email='newemail@example.com' WHERE username='testuser'\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u8a18\u9304\u66f4\u65b0\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-yZTPgKfpkm\">\u522a\u9664\u6578\u64da<\/h3>\n<p>\u522a\u9664\u6578\u64da\u7684\u793a\u4f8b\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<pre><code>&lt;?php\n$sql = \"DELETE FROM users WHERE username='testuser'\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u8a18\u9304\u522a\u9664\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-yZTPgKfpkm\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528\u539f\u751fPHP\u64cd\u4f5c\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9069\u5408\u5404\u7a2e\u898f\u6a21\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u901a\u904e\u4e0a\u8ff0\u793a\u4f8b\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u638c\u63e1\u57fa\u672c\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u6839\u64da\u9700\u6c42\u9032\u884c\u64f4\u5c55\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u539f\u751fPHP\u90fd\u80fd\u63d0\u4f9b\u9748\u6d3b\u6027\u548c\u6027\u80fd\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\u7684PHP\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\u670d\u52d9\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\u7528\u539f\u751fPHP\u9ad8\u6548\u9023\u63a5\u548c\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u7c21\u55ae\u6613\u61c2\u7684\u6b65\u9a5f\u52a9\u4f60\u5feb\u901f\u638c\u63e1\u6578\u64da\u5eab\u7ba1\u7406\u6280\u5de7\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-195495","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195495","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=195495"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195495\/revisions"}],"predecessor-version":[{"id":195496,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195495\/revisions\/195496"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=195495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=195495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=195495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}