{"id":96510,"date":"2024-10-20T10:03:19","date_gmt":"2024-10-20T02:03:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96510\/"},"modified":"2024-10-20T10:03:20","modified_gmt":"2024-10-20T02:03:20","slug":"php%e5%be%8c%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e5%a4%a7%e5%85%a8-%e7%86%9f%e6%82%89php%e5%b0%8d%e5%be%8c%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96510\/","title":{"rendered":"PHP\u5f8c\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u5927\u5168 (\u719f\u6089php\u5c0d\u5f8c\u53f0\u6578\u64da\u5eab\u7684\u64cd\u4f5c)"},"content":{"rendered":"<h1 id=\"php%e5%be%8c%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e5%a4%a7%e5%85%a8-%e7%86%9f%e6%82%89php%e5%b0%8d%e5%be%8c%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%93%8d%e4%bd%9c-NVJuLzHqya\">PHP\u5f8c\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u5927\u5168 (\u719f\u6089PHP\u5c0d\u5f8c\u53f0\u6578\u64da\u5eab\u7684\u64cd\u4f5c)<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cPHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u4f3a\u670d\u5668\u7aef\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5f8c\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u5efa\u7acb\u52d5\u614b\u7db2\u7ad9\u9084\u662f\u958b\u767cWeb\u61c9\u7528\uff0c\u5c0d\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ePHP\u5982\u4f55\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"1-php%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5-NVJuLzHqya\">1. PHP\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002PHP\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u7cfb\u7d71\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fMySQL\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQLi\u64f4\u5c55\u9023\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\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<\/code><\/pre>\n<h2 id=\"2-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e7%9a%84%e5%9f%ba%e6%9c%ac%e9%a1%9e%e5%9e%8b-NVJuLzHqya\">2. \u6578\u64da\u5eab\u64cd\u4f5c\u7684\u57fa\u672c\u985e\u578b<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u6578\u64da\u5eab\u64cd\u4f5c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u57fa\u672c\u985e\u578b\uff1a<\/p>\n<ul>\n<li><strong>\u67e5\u8a62\u6578\u64da (SELECT)<\/strong><\/li>\n<li><strong>\u63d2\u5165\u6578\u64da (INSERT)<\/strong><\/li>\n<li><strong>\u66f4\u65b0\u6578\u64da (UPDATE)<\/strong><\/li>\n<li><strong>\u522a\u9664\u6578\u64da (DELETE)<\/strong><\/li>\n<\/ul>\n<h3 id=\"2-1-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-NVJuLzHqya\">2.1 \u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u67e5\u8a62\u6578\u64da\u662f\u6700\u5e38\u898b\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u67e5\u8a62\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\n$sql = \"SELECT id, name FROM users\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6bcf\u4e00\u884c\u6578\u64da\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"id: \" . $row[\"id\"]. \" - Name: \" . $row[\"name\"]. \"<br>\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n<\/code><\/pre>\n<h3 id=\"2-2-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-NVJuLzHqya\">2.2 \u63d2\u5165\u6578\u64da<\/h3>\n<p>\u63d2\u5165\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\n$sql = \"INSERT INTO users (name) VALUES ('John Doe')\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u65b0\u8a18\u9304\u63d2\u5165\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"<br>\" . $conn-&gt;error;\n}\n<\/code><\/pre>\n<h3 id=\"2-3-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-NVJuLzHqya\">2.3 \u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u66f4\u65b0\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\n$sql = \"UPDATE users SET name='Jane Doe' WHERE id=1\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u8a18\u9304\u66f4\u65b0\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $conn-&gt;error;\n}\n<\/code><\/pre>\n<h3 id=\"2-4-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-NVJuLzHqya\">2.4 \u522a\u9664\u6578\u64da<\/h3>\n<p>\u522a\u9664\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\n$sql = \"DELETE FROM users WHERE id=1\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u8a18\u9304\u522a\u9664\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $conn-&gt;error;\n}\n<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8pdo%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-NVJuLzHqya\">3. \u4f7f\u7528PDO\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u9664\u4e86MySQLi\uff0cPHP\u9084\u652f\u6301PDO\uff08PHP Data Objects\uff09\uff0c\u9019\u662f\u4e00\u500b\u66f4\u9748\u6d3b\u7684\u6578\u64da\u5eab\u8a2a\u554f\u5c64\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\ntry {\n    $pdo = new PDO(\"mysql:host=$servername;dbname=$dbname\", $username, $password);\n    \/\/ \u8a2d\u7f6e\u932f\u8aa4\u6a21\u5f0f\u70ba\u7570\u5e38\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n    \n    \/\/ \u67e5\u8a62\u6578\u64da\n    $stmt = $pdo-&gt;query(\"SELECT id, name FROM users\");\n    while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {\n        echo \"id: \" . $row[\"id\"]. \" - Name: \" . $row[\"name\"]. \"<br>\";\n    }\n} catch(PDOException $e) {\n    echo \"\u9023\u63a5\u5931\u6557: \" . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<h2 id=\"4-%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-NVJuLzHqya\">4. \u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u53ef\u4ee5\u6709\u6548\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u7684\u9810\u8655\u7406\u8a9e\u53e5\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\n$stmt = $pdo-&gt;prepare(\"INSERT INTO users (name) VALUES (:name)\");\n$stmt-&gt;bindParam(':name', $name);\n\n\/\/ \u63d2\u5165\u6578\u64da\n$name = \"John Doe\";\n$stmt-&gt;execute();\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-NVJuLzHqya\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86PHP\u5728\u5f8c\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5305\u62ec\u5982\u4f55\u9023\u63a5\u6578\u64da\u5eab\u3001\u57f7\u884c\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\uff0c\u4ee5\u53ca\u4f7f\u7528PDO\u9032\u884c\u5b89\u5168\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u638c\u63e1\u9019\u4e9b\u57fa\u672c\u6280\u80fd\u5c0d\u65bc\u958b\u767c\u9ad8\u6548\u7684Web\u61c9\u7528\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\uff0c\u63d0\u4f9b\u9ad8\u6548\u80fd\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1PHP\u5f8c\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u5168\u9762\u6307\u5357\uff0c\u5b78\u7fd2\u5982\u4f55\u9ad8\u6548\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u63d0\u5347\u958b\u767c\u6280\u80fd\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-96510","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96510","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=96510"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96510\/revisions"}],"predecessor-version":[{"id":96511,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96510\/revisions\/96511"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}