{"id":96626,"date":"2024-10-20T11:01:19","date_gmt":"2024-10-20T03:01:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96626\/"},"modified":"2024-10-20T11:01:20","modified_gmt":"2024-10-20T03:01:20","slug":"php%e6%95%b8%e6%93%9a%e5%ba%ab%e7%ae%a1%e7%90%86%e5%85%a5%e9%96%80%ef%bc%9a%e5%9f%ba%e7%a4%8e%e7%9f%a5%e8%ad%98%e8%88%87%e6%8a%80%e5%b7%a7-php-%e6%95%b8%e6%93%9a%e5%ba%ab%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96626\/","title":{"rendered":"php\u6578\u64da\u5eab\u7ba1\u7406\u5165\u9580\uff1a\u57fa\u790e\u77e5\u8b58\u8207\u6280\u5de7 (php \u6578\u64da\u5eab\u7ba1\u7406)"},"content":{"rendered":"<h1 id=\"php%e6%95%b8%e6%93%9a%e5%ba%ab%e7%ae%a1%e7%90%86%e5%85%a5%e9%96%80%ef%bc%9a%e5%9f%ba%e7%a4%8e%e7%9f%a5%e8%ad%98%e8%88%87%e6%8a%80%e5%b7%a7-jlzNvwHXIA\">PHP\u6578\u64da\u5eab\u7ba1\u7406\u5165\u9580\uff1a\u57fa\u790e\u77e5\u8b58\u8207\u6280\u5de7<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7ba1\u7406\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002PHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u4f3a\u670d\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u7d93\u5e38\u8207\u6578\u64da\u5eab\u7d50\u5408\u4f7f\u7528\uff0c\u7279\u5225\u662fMySQL\u3002\u672c\u6587\u5c07\u4ecb\u7d39PHP\u6578\u64da\u5eab\u7ba1\u7406\u7684\u57fa\u790e\u77e5\u8b58\u8207\u6280\u5de7\uff0c\u5e6b\u52a9\u521d\u5b78\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"1-%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-jlzNvwHXIA\">1. \u6578\u64da\u5eab\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u6709\u7d44\u7e54\u7684\u6578\u64da\u96c6\u5408\uff0c\u901a\u5e38\u7528\u65bc\u5b58\u5132\u548c\u7ba1\u7406\u4fe1\u606f\u3002\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff08DBMS\uff09\u662f\u7528\u65bc\u5275\u5efa\u3001\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u5eab\u7684\u8edf\u4ef6\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u985e\u578b\u5305\u62ec\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MySQL\u3001PostgreSQL\uff09\u548c\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MongoDB\uff09\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5c08\u6ce8\u65bc\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u7279\u5225\u662fMySQL\u3002<\/p>\n<h2 id=\"2-php%e8%88%87mysql%e7%9a%84%e9%80%a3%e6%8e%a5-jlzNvwHXIA\">2. PHP\u8207MySQL\u7684\u9023\u63a5<\/h2>\n<p>\u8981\u5728PHP\u4e2d\u4f7f\u7528MySQL\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQLi\u64f4\u5c55\u7684\u57fa\u672c\u9023\u63a5\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"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}\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\u8acb\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u66ff\u63db\u4f3a\u670d\u5668\u540d\u7a31\u3001\u7528\u6236\u540d\u3001\u5bc6\u78bc\u548c\u6578\u64da\u5eab\u540d\u7a31\u3002<\/p>\n<h2 id=\"3-%e5%9f%ba%e6%9c%ac%e7%9a%84%e6%95%b8%e6%93%9a%e6%93%8d%e4%bd%9c-jlzNvwHXIA\">3. \u57fa\u672c\u7684\u6578\u64da\u64cd\u4f5c<\/h2>\n<h3 id=\"3-1-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-jlzNvwHXIA\">3.1 \u63d2\u5165\u6578\u64da<\/h3>\n<p>\u63d2\u5165\u6578\u64da\u662f\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u6700\u57fa\u672c\u7684\u529f\u80fd\u4e4b\u4e00\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=\"3-2-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-jlzNvwHXIA\">3.2 \u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u67e5\u8a62\u6578\u64da\u662f\u53e6\u4e00\u500b\u5e38\u898b\u7684\u64cd\u4f5c\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\"]. \" - Name: \" . $row[\"username\"]. \" - Email: \" . $row[\"email\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"3-3-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-jlzNvwHXIA\">3.3 \u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u66f4\u65b0\u6578\u64da\u7684\u793a\u4f8b\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=\"3-4-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-jlzNvwHXIA\">3.4 \u522a\u9664\u6578\u64da<\/h3>\n<p>\u522a\u9664\u6578\u64da\u7684\u793a\u4f8b\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=\"4-%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ae%89%e5%85%a8%e6%80%a7-jlzNvwHXIA\">4. \u6578\u64da\u5eab\u5b89\u5168\u6027<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u5b89\u5168\u63aa\u65bd\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<li>\u5b9a\u671f\u5099\u4efd\u6578\u64da\u5eab\u3002<\/li>\n<li>\u9650\u5236\u6578\u64da\u5eab\u7528\u6236\u7684\u6b0a\u9650\u3002<\/li>\n<\/ul>\n<h2 id=\"5-%e7%b8%bd%e7%b5%90-jlzNvwHXIA\">5. \u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86PHP\u6578\u64da\u5eab\u7ba1\u7406\u7684\u57fa\u672c\u77e5\u8b58\u8207\u6280\u5de7\uff0c\u5305\u62ec\u5982\u4f55\u9023\u63a5MySQL\u6578\u64da\u5eab\u3001\u57f7\u884c\u57fa\u672c\u7684\u6578\u64da\u64cd\u4f5c\u4ee5\u53ca\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u5b89\u5168\u6027\u3002\u638c\u63e1\u9019\u4e9b\u57fa\u790e\u77e5\u8b58\u5f8c\uff0c\u60a8\u5c07\u80fd\u5920\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u70ba\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u63d0\u4f9b\u7a69\u5b9a\u7684\u6578\u64da\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bcVPS\u6216\u5176\u4ed6\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\u611f\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\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\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2PHP\u6578\u64da\u5eab\u7ba1\u7406\u7684\u57fa\u790e\u77e5\u8b58\u8207\u6280\u5de7\uff0c\u638c\u63e1\u6578\u64da\u64cd\u4f5c\u3001\u67e5\u8a62\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u8f15\u9b06\u7ba1\u7406\u60a8\u7684\u6578\u64da\u5eab\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-96626","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96626","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=96626"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96626\/revisions"}],"predecessor-version":[{"id":96627,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96626\/revisions\/96627"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}