{"id":96598,"date":"2024-10-20T10:47:15","date_gmt":"2024-10-20T02:47:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96598\/"},"modified":"2024-10-20T10:47:16","modified_gmt":"2024-10-20T02:47:16","slug":"php%e5%af%a6%e7%8f%be%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%97%e6%ae%b5%e5%80%bc%e7%9a%84%e6%96%b9%e6%b3%95%e7%b0%a1%e4%bb%8b-php%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96598\/","title":{"rendered":"php\u5be6\u73fe\u7372\u53d6\u6578\u64da\u5eab\u5b57\u6bb5\u503c\u7684\u65b9\u6cd5\u7c21\u4ecb (php\u8b80\u53d6\u6578\u64da\u5eab\u5b57\u6bb5\u503c)"},"content":{"rendered":"<h1 id=\"php%e5%af%a6%e7%8f%be%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%97%e6%ae%b5%e5%80%bc%e7%9a%84%e6%96%b9%e6%b3%95%e7%b0%a1%e4%bb%8b-QhUjUoOtUT\">PHP\u5be6\u73fe\u7372\u53d6\u6578\u64da\u5eab\u5b57\u6bb5\u503c\u7684\u65b9\u6cd5\u7c21\u4ecb<\/h1>\n<p>\u5728\u73fe\u4ee3\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\u5f9eMySQL\u3001PostgreSQL\u9084\u662f\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\uff0cPHP\u90fd\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5e7e\u7a2e\u5e38\u898b\u7684PHP\u8b80\u53d6\u6578\u64da\u5eab\u5b57\u6bb5\u503c\u7684\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8mysqli%e6%93%b4%e5%b1%95-QhUjUoOtUT\">1. \u4f7f\u7528MySQLi\u64f4\u5c55<\/h2>\n<p>MySQLi\uff08MySQL Improved\uff09\u662fPHP\u7684\u4e00\u500b\u64f4\u5c55\uff0c\u63d0\u4f9b\u4e86\u5c0dMySQL\u6578\u64da\u5eab\u7684\u652f\u6301\u3002\u4f7f\u7528MySQLi\u53ef\u4ee5\u901a\u904e\u9762\u5411\u5c0d\u8c61\u6216\u904e\u7a0b\u5316\u7684\u65b9\u5f0f\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQLi\u8b80\u53d6\u6578\u64da\u5eab\u5b57\u6bb5\u503c\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\n$connection = new mysqli(\"localhost\", \"username\", \"password\", \"database\");\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($connection-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $connection-&gt;connect_error);\n}\n\n\/\/ \u67e5\u8a62\u6578\u64da\n$sql = \"SELECT field_name FROM table_name\";\n$result = $connection-&gt;query($sql);\n\n\/\/ \u7372\u53d6\u6578\u64da\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"\u5b57\u6bb5\u503c: \" . $row[\"field_name\"] . \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n\n\/\/ \u95dc\u9589\u9023\u63a5\n$connection-&gt;close();\n?&gt;<\/code><\/pre>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8pdo%e6%93%b4%e5%b1%95-QhUjUoOtUT\">2. \u4f7f\u7528PDO\u64f4\u5c55<\/h2>\n<p>PDO\uff08PHP Data Objects\uff09\u662f\u4e00\u500b\u66f4\u901a\u7528\u7684\u6578\u64da\u5eab\u8a2a\u554f\u5c64\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u3002\u4f7f\u7528PDO\u7684\u597d\u8655\u5728\u65bc\u5b83\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u5b89\u5168\u6027\u548c\u9748\u6d3b\u6027\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u8b80\u53d6\u6578\u64da\u5eab\u5b57\u6bb5\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\ntry {\n    $pdo = new PDO(\"mysql:host=localhost;dbname=database\", \"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;prepare(\"SELECT field_name FROM table_name\");\n    $stmt-&gt;execute();\n\n    \/\/ \u7372\u53d6\u6578\u64da\n    while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {\n        echo \"\u5b57\u6bb5\u503c: \" . $row[\"field_name\"] . \"&lt;br&gt;\";\n    }\n} catch (PDOException $e) {\n    echo \"\u932f\u8aa4: \" . $e-&gt;getMessage();\n}\n\n\/\/ \u95dc\u9589\u9023\u63a5\n$pdo = null;\n?&gt;<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8orm%e6%a1%86%e6%9e%b6-QhUjUoOtUT\">3. \u4f7f\u7528ORM\u6846\u67b6<\/h2>\n<p>\u5c0d\u65bc\u5927\u578b\u61c9\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7528ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\u53ef\u4ee5\u5927\u5927\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u5e38\u898b\u7684PHP ORM\u6846\u67b6\u5982Eloquent\u548cDoctrine\uff0c\u9019\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c64\u6b21\u7684\u62bd\u8c61\uff0c\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c0\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Eloquent\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\nuse IlluminateDatabaseCapsuleManager as Capsule;\n\n\/\/ \u8a2d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\n$capsule = new Capsule;\n$capsule-&gt;addConnection([\n    'driver' =&gt; 'mysql',\n    'host' =&gt; 'localhost',\n    'database' =&gt; 'database',\n    'username' =&gt; 'username',\n    'password' =&gt; 'password',\n    'charset' =&gt; 'utf8',\n    'collation' =&gt; 'utf8_unicode_ci',\n    'prefix' =&gt; '',\n]);\n\n\/\/ \u555f\u52d5Eloquent\n$capsule-&gt;setAsGlobal();\n$capsule-&gt;bootEloquent();\n\n\/\/ \u67e5\u8a62\u6578\u64da\n$results = Capsule::table('table_name')-&gt;select('field_name')-&gt;get();\nforeach ($results as $row) {\n    echo \"\u5b57\u6bb5\u503c: \" . $row-&gt;field_name . \"&lt;br&gt;\";\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-QhUjUoOtUT\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5e7e\u7a2e\u4f7f\u7528PHP\u8b80\u53d6\u6578\u64da\u5eab\u5b57\u6bb5\u503c\u7684\u65b9\u6cd5\uff0c\u5305\u62ecMySQLi\u3001PDO\u548cORM\u6846\u67b6\u3002\u9019\u4e9b\u65b9\u6cd5\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u8a2a\u554f\u65b9\u5f0f\u90fd\u80fd\u63d0\u9ad8\u958b\u767c\u6548\u7387\u548c\u4ee3\u78bc\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684PHP\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528PHP\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u5b57\u6bb5\u503c\uff0c\u7c21\u55ae\u6613\u61c2\u7684\u65b9\u6cd5\u4ecb\u7d39\uff0c\u52a9\u60a8\u8f15\u9b06\u8b80\u53d6\u6578\u64da\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-96598","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96598","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=96598"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96598\/revisions"}],"predecessor-version":[{"id":96599,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96598\/revisions\/96599"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}