{"id":96662,"date":"2024-10-20T11:19:17","date_gmt":"2024-10-20T03:19:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96662\/"},"modified":"2024-10-20T11:19:18","modified_gmt":"2024-10-20T03:19:18","slug":"php%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%96%b9%e6%b3%95-php-%e6%95%b8%e6%93%9a%e5%ba%ab-%e9%80%a3%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96662\/","title":{"rendered":"php\u8207\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u65b9\u6cd5 (php \u6578\u64da\u5eab \u9023\u63a5)"},"content":{"rendered":"<h1 id=\"php%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%96%b9%e6%b3%95-VBZOYagfVX\">PHP\u8207\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u65b9\u6cd5<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cPHP\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\u7121\u8ad6\u662f\u5efa\u7acb\u52d5\u614b\u7db2\u7ad9\u9084\u662f\u958b\u767c\u61c9\u7528\u7a0b\u5f0f\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u672c\u6587\u5c07\u63a2\u8a0ePHP\u8207\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a1%9e%e5%9e%8b-VBZOYagfVX\">\u5e38\u898b\u7684\u6578\u64da\u5eab\u985e\u578b<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u6578\u64da\u5eab\u985e\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>MySQL<\/strong>\uff1a\u958b\u6e90\u4e14\u5ee3\u6cdb\u4f7f\u7528\u7684\u6578\u64da\u5eab\uff0c\u9069\u5408\u4e2d\u5c0f\u578b\u61c9\u7528\u3002<\/li>\n<li><strong>PostgreSQL<\/strong>\uff1a\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u6578\u64da\u5eab\uff0c\u652f\u6301\u8907\u96dc\u67e5\u8a62\u548c\u6578\u64da\u5b8c\u6574\u6027\u3002<\/li>\n<li><strong>SQLite<\/strong>\uff1a\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u5eab\uff0c\u9069\u5408\u5c0f\u578b\u61c9\u7528\u548c\u958b\u767c\u6e2c\u8a66\u3002<\/li>\n<\/ul>\n<h2 id=\"php%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab-VBZOYagfVX\">PHP\u9023\u63a5MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-VBZOYagfVX\">1. \u5275\u5efa\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u4f7f\u7528PHP\u7684\u5167\u5efa\u51fd\u6578\u4f86\u5275\u5efa\u6578\u64da\u5eab\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528mysqli\u64f4\u5c55\u7684\u7bc4\u4f8b\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<h3 id=\"2-%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2-VBZOYagfVX\">2. \u57f7\u884c\u67e5\u8a62<\/h3>\n<p>\u4e00\u65e6\u9023\u63a5\u6210\u529f\uff0c\u5c31\u53ef\u4ee5\u57f7\u884cSQL\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\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\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"3-%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-VBZOYagfVX\">3. \u95dc\u9589\u9023\u63a5<\/h3>\n<p>\u5728\u5b8c\u6210\u6578\u64da\u5eab\u64cd\u4f5c\u5f8c\uff0c\u61c9\u8a72\u95dc\u9589\u9023\u63a5\uff1a<\/p>\n<pre><code>&lt;?php\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8pdo%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-VBZOYagfVX\">\u4f7f\u7528PDO\u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>\u9664\u4e86mysqli\uff0cPHP\u9084\u63d0\u4f9b\u4e86PDO\uff08PHP Data Objects\uff09\u64f4\u5c55\uff0c\u9019\u662f\u4e00\u500b\u66f4\u9748\u6d3b\u7684\u6578\u64da\u5eab\u8a2a\u554f\u5c64\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u9023\u63a5MySQL\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\ntry {\n    $conn = new PDO(\"mysql:host=$servername;dbname=$dbname\", $username, $password);\n    \/\/ \u8a2d\u7f6ePDO\u932f\u8aa4\u6a21\u5f0f\u70ba\u7570\u5e38\n    $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n    echo \"\u9023\u63a5\u6210\u529f\";\n} catch(PDOException $e) {\n    echo \"\u9023\u63a5\u5931\u6557: \" . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VBZOYagfVX\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u63a2\u8a0e\u4e86PHP\u8207\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528mysqli\u548cPDO\u5169\u7a2e\u65b9\u5f0f\u3002\u9019\u4e9b\u6280\u8853\u4e0d\u50c5\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\uff0c\u9084\u80fd\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u4f3a\u670d\u5668\u74b0\u5883\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u5c0d\u65bc<em><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/em>\u6216<em><a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a><\/em>\u6709\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3PHP\u8207\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u64cd\u4f5c\uff0c\u63d0\u5347\u7db2\u7ad9\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-96662","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96662","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=96662"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96662\/revisions"}],"predecessor-version":[{"id":96663,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96662\/revisions\/96663"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}