{"id":157044,"date":"2024-11-02T21:18:23","date_gmt":"2024-11-02T13:18:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/157044\/"},"modified":"2024-11-02T21:18:24","modified_gmt":"2024-11-02T13:18:24","slug":"%e5%88%86%e4%ba%ab-phpmysql-%e9%96%8b%e7%99%bc%e5%9f%ba%e7%a4%8e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/157044\/","title":{"rendered":"\u5206\u4eab PHP+MySQL \u958b\u767c\u57fa\u790e"},"content":{"rendered":"<h1 id=\"%e5%88%86%e4%ba%ab-phpmysql-%e9%96%8b%e7%99%bc%e5%9f%ba%e7%a4%8e-FEOstESNqo\">\u5206\u4eab PHP+MySQL \u958b\u767c\u57fa\u790e<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cPHP \u548c MySQL \u662f\u6700\u53d7\u6b61\u8fce\u7684\u6280\u8853\u7d44\u5408\u4e4b\u4e00\u3002PHP \u662f\u4e00\u7a2e\u4f3a\u670d\u5668\u7aef\u7684\u8173\u672c\u8a9e\u8a00\uff0c\u800c MySQL \u5247\u662f\u4e00\u500b\u958b\u6e90\u7684\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u9019\u5169\u8005\u7684\u7d50\u5408\u4f7f\u5f97\u958b\u767c\u52d5\u614b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u8b8a\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u9748\u6d3b\u3002\u672c\u6587\u5c07\u4ecb\u7d39 PHP \u548c MySQL \u7684\u57fa\u672c\u6982\u5ff5\u53ca\u5176\u958b\u767c\u6d41\u7a0b\uff0c\u5e6b\u52a9\u521d\u5b78\u8005\u5feb\u901f\u5165\u9580\u3002<\/p>\n<h2 id=\"php-%e7%b0%a1%e4%bb%8b-FEOstESNqo\">PHP \u7c21\u4ecb<\/h2>\n<p>PHP\uff08Hypertext Preprocessor\uff09\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u4f3a\u670d\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u7279\u5225\u9069\u5408\u65bc\u7db2\u9801\u958b\u767c\u3002PHP \u7684\u8a9e\u6cd5\u7c21\u55ae\u6613\u5b78\uff0c\u4e26\u4e14\u80fd\u5920\u8207 HTML \u7121\u7e2b\u96c6\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 PHP \u7bc4\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\necho \"Hello, World!\";\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0cPHP \u4f7f\u7528 echo \u8a9e\u53e5\u8f38\u51fa &#8220;Hello, World!&#8221; \u5230\u700f\u89bd\u5668\u3002\u9019\u53ea\u662f PHP \u7684\u57fa\u672c\u7528\u6cd5\uff0c\u96a8\u8457\u5b78\u7fd2\u7684\u6df1\u5165\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 PHP \u4f86\u8655\u7406\u8868\u55ae\u6578\u64da\u3001\u7ba1\u7406\u6703\u8a71\u3001\u9023\u63a5\u6578\u64da\u5eab\u7b49\u3002<\/p>\n<h2 id=\"mysql-%e7%b0%a1%e4%bb%8b-FEOstESNqo\">MySQL \u7c21\u4ecb<\/h2>\n<p>MySQL \u662f\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u5b83\u4f7f\u7528 SQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u4f86\u9032\u884c\u6578\u64da\u7684\u67e5\u8a62\u548c\u7ba1\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 SQL \u67e5\u8a62\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>SELECT * FROM users WHERE age &gt; 18;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u5f9e users \u8868\u4e2d\u9078\u64c7\u6240\u6709\u5e74\u9f61\u5927\u65bc 18 \u7684\u7528\u6236\u3002MySQL \u7684\u5f37\u5927\u4e4b\u8655\u5728\u65bc\u5176\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u8a2d\u8a08\u3002<\/p>\n<h2 id=\"php-%e8%88%87-mysql-%e7%9a%84%e7%b5%90%e5%90%88-FEOstESNqo\">PHP \u8207 MySQL \u7684\u7d50\u5408<\/h2>\n<p>PHP \u548c MySQL \u7684\u7d50\u5408\u4f7f\u5f97\u958b\u767c\u52d5\u614b\u7db2\u7ad9\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 PHP \u4f86\u8655\u7406\u7528\u6236\u8acb\u6c42\uff0c\u4e26\u901a\u904e MySQL \u4f86\u5b58\u5132\u548c\u6aa2\u7d22\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 PHP \u9023\u63a5 MySQL \u6578\u64da\u5eab\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\u9996\u5148\u5b9a\u7fa9\u4e86\u6578\u64da\u5eab\u7684\u9023\u63a5\u53c3\u6578\uff0c\u7136\u5f8c\u4f7f\u7528 mysqli \u985e\u4f86\u5275\u5efa\u9023\u63a5\u3002\u5982\u679c\u9023\u63a5\u6210\u529f\uff0c\u5247\u8f38\u51fa &#8220;\u9023\u63a5\u6210\u529f&#8221;\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e7%9a%84-crud-%e6%93%8d%e4%bd%9c-FEOstESNqo\">\u57fa\u672c\u7684 CRUD \u64cd\u4f5c<\/h2>\n<p>\u5728\u4f7f\u7528 PHP \u548c MySQL \u958b\u767c\u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u6700\u5e38\u898b\u7684\u64cd\u4f5c\u662f CRUD\uff08\u5275\u5efa\u3001\u8b80\u53d6\u3001\u66f4\u65b0\u3001\u522a\u9664\uff09\u3002\u4ee5\u4e0b\u662f\u9019\u4e9b\u64cd\u4f5c\u7684\u7c21\u8981\u4ecb\u7d39\uff1a<\/p>\n<ul>\n<li><strong>\u5275\u5efa\uff08Create\uff09<\/strong>: \u4f7f\u7528 INSERT \u8a9e\u53e5\u5c07\u65b0\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002<\/li>\n<li><strong>\u8b80\u53d6\uff08Read\uff09<\/strong>: \u4f7f\u7528 SELECT \u8a9e\u53e5\u5f9e\u6578\u64da\u5eab\u4e2d\u6aa2\u7d22\u6578\u64da\u3002<\/li>\n<li><strong>\u66f4\u65b0\uff08Update\uff09<\/strong>: \u4f7f\u7528 UPDATE \u8a9e\u53e5\u4fee\u6539\u6578\u64da\u5eab\u4e2d\u7684\u73fe\u6709\u6578\u64da\u3002<\/li>\n<li><strong>\u522a\u9664\uff08Delete\uff09<\/strong>: \u4f7f\u7528 DELETE \u8a9e\u53e5\u5f9e\u6578\u64da\u5eab\u4e2d\u522a\u9664\u6578\u64da\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u63d2\u5165\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$sql = \"INSERT INTO users (name, age) VALUES ('John Doe', 25)\";\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<h2 id=\"%e7%b8%bd%e7%b5%90-FEOstESNqo\">\u7e3d\u7d50<\/h2>\n<p>PHP \u548c MySQL \u7684\u7d50\u5408\u70ba\u7db2\u9801\u958b\u767c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u652f\u6301\uff0c\u7121\u8ad6\u662f\u5c0f\u578b\u7db2\u7ad9\u9084\u662f\u5927\u578b\u61c9\u7528\u7a0b\u5e8f\uff0c\u90fd\u80fd\u5920\u9ad8\u6548\u5730\u8655\u7406\u6578\u64da\u3002\u638c\u63e1\u9019\u4e9b\u57fa\u790e\u77e5\u8b58\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9032\u4e00\u6b65\u63a2\u7d22\u66f4\u9ad8\u7d1a\u7684\u529f\u80fd\uff0c\u5982\u5b89\u5168\u6027\u3001\u6027\u80fd\u512a\u5316\u7b49\u3002\u82e5\u60a8\u6709\u8208\u8da3\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u5728\u9999\u6e2f\u4f7f\u7528 <a href=\"https:\/\/server.hk\">VPS<\/a> \u9032\u884c PHP \u548c MySQL \u958b\u767c\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2 PHP \u548c MySQL \u7684\u958b\u767c\u57fa\u790e\uff0c\u638c\u63e1\u7db2\u7ad9\u5efa\u8a2d\u548c\u6578\u64da\u5eab\u7ba1\u7406\u7684\u6838\u5fc3\u6280\u80fd\uff0c\u5feb\u901f\u63d0\u5347\u7de8\u7a0b\u80fd\u529b\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-157044","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/157044","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=157044"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/157044\/revisions"}],"predecessor-version":[{"id":157045,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/157044\/revisions\/157045"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=157044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=157044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=157044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}