{"id":96472,"date":"2024-10-20T09:44:17","date_gmt":"2024-10-20T01:44:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96472\/"},"modified":"2024-10-20T09:44:17","modified_gmt":"2024-10-20T01:44:17","slug":"php%e8%ae%8a%e9%87%8f%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96%e5%ad%98%e5%84%b2-php%e8%ae%8a%e9%87%8f%e5%af%ab%e5%85%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96472\/","title":{"rendered":"PHP\u8b8a\u91cf\u5beb\u5165\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u6301\u4e45\u5316\u5b58\u5132 (php\u8b8a\u91cf\u5beb\u5165\u5beb\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"php%e8%ae%8a%e9%87%8f%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96%e5%ad%98%e5%84%b2-yHZXlEUwDo\">PHP\u8b8a\u91cf\u5beb\u5165\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u6301\u4e45\u5316\u5b58\u5132<\/h1>\n<p>\u5728\u73fe\u4ee3\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u7684\u6301\u4e45\u5316\u5b58\u5132\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002PHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u4f3a\u670d\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u7d93\u5e38\u88ab\u7528\u4f86\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528PHP\u5c07\u8b8a\u91cf\u5beb\u5165\u6578\u64da\u5eab\uff0c\u4ee5\u5be6\u73fe\u6578\u64da\u7684\u6301\u4e45\u5316\u5b58\u5132\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%81%b8%e6%93%87-yHZXlEUwDo\">\u6578\u64da\u5eab\u7684\u9078\u64c7<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9078\u64c7\u4e00\u500b\u5408\u9069\u7684\u6578\u64da\u5eab\u3002\u5e38\u898b\u7684\u9078\u64c7\u5305\u62ecMySQL\u3001PostgreSQL\u548cSQLite\u7b49\u3002\u9019\u4e9b\u6578\u64da\u5eab\u90fd\u652f\u6301SQL\u8a9e\u8a00\uff0c\u4e26\u4e14\u80fd\u5920\u8207PHP\u7121\u7e2b\u96c6\u6210\u3002\u672c\u6587\u5c07\u4ee5MySQL\u70ba\u4f8b\u9032\u884c\u8aaa\u660e\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e8%a8%ad%e7%bd%ae-yHZXlEUwDo\">\u74b0\u5883\u8a2d\u7f6e<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86PHP\u548cMySQL\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528XAMPP\u6216MAMP\u7b49\u96c6\u6210\u74b0\u5883\u4f86\u5feb\u901f\u642d\u5efa\u958b\u767c\u74b0\u5883\u3002<\/p>\n<h3 id=\"%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e5%92%8c%e8%a1%a8-yHZXlEUwDo\">\u5275\u5efa\u6578\u64da\u5eab\u548c\u8868<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u6578\u64da\u5eab\u548c\u4e00\u500b\u8868\u4f86\u5b58\u5132\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u6578\u64da\u5eab\u548c\u8868\u7684SQL\u8a9e\u53e5\uff1a<\/p>\n<pre><code>CREATE DATABASE my_database;\n\nUSE my_database;\n\nCREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100) NOT NULL,\n    email VARCHAR(100) NOT NULL\n);<\/code><\/pre>\n<h2 id=\"php%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-yHZXlEUwDo\">PHP\u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5728PHP\u4e2d\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u53ef\u4ee5\u4f7f\u7528mysqli\u6216PDO\u64f4\u5c55\u4f86\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528mysqli\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"my_database\";\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?&gt;<\/code><\/pre>\n<h2 id=\"%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a-yHZXlEUwDo\">\u5beb\u5165\u6578\u64da<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u60a8\u5c31\u53ef\u4ee5\u958b\u59cb\u5beb\u5165\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u5c07PHP\u8b8a\u91cf\u5beb\u5165\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$name = \"John Doe\";\n$email = \"john@example.com\";\n\n\/\/ \u6e96\u5099SQL\u8a9e\u53e5\n$sql = \"INSERT INTO users (name, email) VALUES (?, ?)\";\n\n\/\/ \u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u9632\u6b62SQL\u6ce8\u5165\n$stmt = $conn-&gt;prepare($sql);\n$stmt-&gt;bind_param(\"ss\", $name, $email);\n\n\/\/ \u57f7\u884c\u8a9e\u53e5\nif ($stmt-&gt;execute()) {\n    echo \"\u65b0\u8a18\u9304\u63d2\u5165\u6210\u529f\";\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<h2 id=\"%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96%e7%9a%84%e6%84%8f%e7%be%a9-yHZXlEUwDo\">\u6578\u64da\u6301\u4e45\u5316\u7684\u610f\u7fa9<\/h2>\n<p>\u6578\u64da\u6301\u4e45\u5316\u662f\u6307\u5c07\u6578\u64da\u5b58\u5132\u5728\u975e\u6613\u5931\u6027\u5b58\u5132\u4ecb\u8cea\u4e2d\uff0c\u4ee5\u4fbf\u5728\u61c9\u7528\u7a0b\u5e8f\u91cd\u555f\u6216\u7cfb\u7d71\u5d29\u6f70\u5f8c\u4ecd\u7136\u80fd\u5920\u8a2a\u554f\u9019\u4e9b\u6578\u64da\u3002\u9019\u5c0d\u65bc\u7528\u6236\u4fe1\u606f\u3001\u4ea4\u6613\u8a18\u9304\u7b49\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u4f7f\u7528PHP\u8207\u6578\u64da\u5eab\u7684\u7d50\u5408\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u7684\u6301\u4e45\u5316\u5b58\u5132\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yHZXlEUwDo\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528PHP\u5c07\u8b8a\u91cf\u5beb\u5165\u6578\u64da\u5eab\uff0c\u4ee5\u5be6\u73fe\u6578\u64da\u7684\u6301\u4e45\u5316\u5b58\u5132\u3002\u901a\u904e\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3001\u8a2d\u7f6e\u74b0\u5883\u3001\u9023\u63a5\u6578\u64da\u5eab\u4ee5\u53ca\u5beb\u5165\u6578\u64da\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u5132\u61c9\u7528\u7a0b\u5e8f\u4e2d\u7684\u6578\u64da\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u7ba1\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u5c07\u662f\u660e\u667a\u4e4b\u8209\uff0c\u56e0\u70ba\u5b83\u80fd\u63d0\u4f9b\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u8cc7\u6e90\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528PHP\u5c07\u8b8a\u91cf\u5beb\u5165\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u6301\u4e45\u5316\u5b58\u5132\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u8207\u61c9\u7528\u958b\u767c\u904e\u7a0b\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-96472","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96472","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=96472"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96472\/revisions"}],"predecessor-version":[{"id":96473,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96472\/revisions\/96473"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}