{"id":96422,"date":"2024-10-20T09:17:18","date_gmt":"2024-10-20T01:17:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96422\/"},"modified":"2024-10-20T09:17:18","modified_gmt":"2024-10-20T01:17:18","slug":"php-mysql%e5%ba%ab%e5%87%bd%e6%95%b8%e5%85%a8%e9%9d%a2%e5%a4%a7%e6%8f%ad%e5%af%86%ef%bc%81-php-mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e5%87%bd%e6%95%b8%e5%a4%a7%e5%85%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96422\/","title":{"rendered":"PHP MySQL\u5eab\u51fd\u6578\u5168\u9762\u5927\u63ed\u5bc6\uff01 (php mysql\u6578\u64da\u5eab\u51fd\u6578\u5927\u5168)"},"content":{"rendered":"<h1 id=\"php-mysql%e5%ba%ab%e5%87%bd%e6%95%b8%e5%85%a8%e9%9d%a2%e5%a4%a7%e6%8f%ad%e5%af%86%ef%bc%81-afOimBwbpm\">PHP MySQL\u5eab\u51fd\u6578\u5168\u9762\u5927\u63ed\u5bc6\uff01<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cPHP\u548cMySQL\u7684\u7d50\u5408\u662f\u6700\u5e38\u898b\u7684\u6280\u8853\u4e4b\u4e00\u3002PHP\u4f5c\u70ba\u4e00\u7a2e\u4f3a\u670d\u5668\u7aef\u7684\u8173\u672c\u8a9e\u8a00\uff0c\u8207MySQL\u6578\u64da\u5eab\u7684\u914d\u5408\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u8655\u7406\u6578\u64da\u3002\u672c\u6587\u5c07\u5168\u9762\u4ecb\u7d39PHP\u4e2d\u8207MySQL\u76f8\u95dc\u7684\u5eab\u51fd\u6578\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u9019\u4e9b\u51fd\u6578\u3002<\/p>\n<h2 id=\"1-mysql%e6%93%b4%e5%b1%95%e7%9a%84%e6%ad%b7%e5%8f%b2%e8%83%8c%e6%99%af-afOimBwbpm\">1. MySQL\u64f4\u5c55\u7684\u6b77\u53f2\u80cc\u666f<\/h2>\n<p>\u6700\u521d\uff0cPHP\u4f7f\u7528\u7684\u662fMySQL\u64f4\u5c55\uff08mysql_\u7cfb\u5217\u51fd\u6578\uff09\uff0c\u4f46\u7531\u65bc\u5176\u4e0d\u652f\u6301\u9810\u8655\u7406\u8a9e\u53e5\u548c\u5c0d\u932f\u8aa4\u8655\u7406\u7684\u652f\u6301\u4e0d\u8db3\uff0c\u5f9ePHP 5.5\u958b\u59cb\uff0c\u9019\u4e9b\u51fd\u6578\u88ab\u6a19\u8a18\u70ba\u904e\u6642\u3002\u96a8\u5f8c\uff0cMySQLi\uff08MySQL Improved\uff09\u548cPDO\uff08PHP Data Objects\uff09\u6210\u70ba\u4e86\u65b0\u7684\u6a19\u6e96\u3002<\/p>\n<h2 id=\"2-mysqli%e5%87%bd%e6%95%b8-afOimBwbpm\">2. MySQLi\u51fd\u6578<\/h2>\n<p>MySQLi\u662f\u5c0dMySQL\u7684\u6539\u9032\u7248\u672c\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5c0d\u8c61\u548c\u7a0b\u5e8f\u5316\u7684\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684MySQLi\u51fd\u6578\uff1a<\/p>\n<ul>\n<li><code>mysqli_connect()<\/code>\uff1a\u7528\u65bc\u5efa\u7acb\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/li>\n<li><code>mysqli_query()<\/code>\uff1a\u7528\u65bc\u57f7\u884cSQL\u67e5\u8a62\u3002<\/li>\n<li><code>mysqli_fetch_assoc()<\/code>\uff1a\u7528\u65bc\u5f9e\u7d50\u679c\u96c6\u4e2d\u7372\u53d6\u95dc\u806f\u6578\u7d44\u3002<\/li>\n<li><code>mysqli_close()<\/code>\uff1a\u7528\u65bc\u95dc\u9589\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc%ef%bc%9a-afOimBwbpm\">\u793a\u4f8b\u4ee3\u78bc\uff1a<\/h3>\n<pre><code>\n$connection = mysqli_connect(\"localhost\", \"username\", \"password\", \"database\");\n\nif (!$connection) {\n    die(\"Connection failed: \" . mysqli_connect_error());\n}\n\n$sql = \"SELECT id, name FROM users\";\n$result = mysqli_query($connection, $sql);\n\nif (mysqli_num_rows($result) &gt; 0) {\n    while($row = mysqli_fetch_assoc($result)) {\n        echo \"id: \" . $row[\"id\"]. \" - Name: \" . $row[\"name\"]. \"<br>\";\n    }\n} else {\n    echo \"0 results\";\n}\n\nmysqli_close($connection);\n<\/code><\/pre>\n<h2 id=\"3-pdo%e5%87%bd%e6%95%b8-afOimBwbpm\">3. PDO\u51fd\u6578<\/h2>\n<p>PDO\u662f\u4e00\u500b\u6578\u64da\u8a2a\u554f\u62bd\u8c61\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\u4e00\u4e9b\u5e38\u7528\u7684PDO\u51fd\u6578\uff1a<\/p>\n<ul>\n<li><code>new PDO()<\/code>\uff1a\u7528\u65bc\u5275\u5efaPDO\u5be6\u4f8b\u3002<\/li>\n<li><code>prepare()<\/code>\uff1a\u7528\u65bc\u6e96\u5099SQL\u8a9e\u53e5\u3002<\/li>\n<li><code>execute()<\/code>\uff1a\u7528\u65bc\u57f7\u884c\u6e96\u5099\u597d\u7684\u8a9e\u53e5\u3002<\/li>\n<li><code>fetch()<\/code>\uff1a\u7528\u65bc\u7372\u53d6\u7d50\u679c\u96c6\u4e2d\u7684\u4e00\u884c\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc%ef%bc%9a-afOimBwbpm\">\u793a\u4f8b\u4ee3\u78bc\uff1a<\/h3>\n<pre><code>\ntry {\n    $pdo = new PDO(\"mysql:host=localhost;dbname=database\", \"username\", \"password\");\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    $stmt = $pdo-&gt;prepare(\"SELECT id, name FROM users\");\n    $stmt-&gt;execute();\n\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 \"Connection failed: \" . $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-afOimBwbpm\">4. \u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u4f7f\u7528PHP\u8207MySQL\u9032\u884c\u6578\u64da\u64cd\u4f5c\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u958b\u767c\u8005\u61c9\u8a72\u907f\u514d\u4f7f\u7528\u4e0d\u5b89\u5168\u7684SQL\u67e5\u8a62\uff0c\u4e26\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002\u7121\u8ad6\u662f\u4f7f\u7528MySQLi\u9084\u662fPDO\uff0c\u9810\u8655\u7406\u8a9e\u53e5\u90fd\u662f\u4e00\u500b\u6709\u6548\u7684\u9632\u8b77\u63aa\u65bd\u3002<\/p>\n<h2 id=\"5-%e7%b8%bd%e7%b5%90-afOimBwbpm\">5. \u7e3d\u7d50<\/h2>\n<p>PHP\u8207MySQL\u7684\u7d50\u5408\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002\u7121\u8ad6\u662f\u4f7f\u7528MySQLi\u9084\u662fPDO\uff0c\u4e86\u89e3\u9019\u4e9b\u5eab\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u96a8\u8457\u6280\u8853\u7684\u767c\u5c55\uff0c\u958b\u767c\u8005\u61c9\u6301\u7e8c\u5b78\u7fd2\u548c\u9069\u61c9\u65b0\u7684\u6280\u8853\uff0c\u4ee5\u63d0\u9ad8\u958b\u767c\u6548\u7387\u548c\u61c9\u7528\u5b89\u5168\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u500b\u4eba\u9805\u76ee\u9084\u662f\u4f01\u696d\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22PHP MySQL\u5eab\u51fd\u6578\u7684\u5168\u9762\u6307\u5357\uff0c\u6df1\u5165\u4e86\u89e3\u6578\u64da\u5eab\u64cd\u4f5c\u3001\u6700\u4f73\u5be6\u8e10\u53ca\u5e38\u7528\u51fd\u6578\uff0c\u63d0\u5347\u4f60\u7684\u958b\u767c\u6280\u80fd\uff01<\/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-96422","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96422","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=96422"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96422\/revisions"}],"predecessor-version":[{"id":96423,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96422\/revisions\/96423"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}