{"id":27507,"date":"2024-11-24T16:13:46","date_gmt":"2024-11-24T08:13:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27507\/"},"modified":"2024-11-24T16:13:46","modified_gmt":"2024-11-24T08:13:46","slug":"php%e6%80%8e%e4%b9%88%e8%bf%9e%e6%8e%a5mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e9%87%8c%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27507\/","title":{"rendered":"php\u600e\u4e48\u8fde\u63a5mysql\u6570\u636e\u5e93\u91cc\u7684\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 php \u8fde\u63a5 mysql \u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528 mysqli_connect() \u51fd\u6570\u5efa\u7acb\u8fde\u63a5\u3002\u6267\u884c\u67e5\u8be2\uff0c\u4f7f\u7528 mysqli_query() \u51fd\u6570\u4f20\u9012 sql \u67e5\u8be2\u3002\u5904\u7406\u7ed3\u679c\uff0c\u4f7f\u7528 mysqli_fetch_assoc()\u3001mysqli_fetch_row() \u6216 mysqli_fetch_object() \u83b7\u53d6\u5173\u8054\u6570\u7ec4\u3001\u6570\u503c\u6570\u7ec4\u6216\u5bf9\u8c61\u5f62\u5f0f\u7684\u7ed3\u679c\u3002\u5173\u95ed\u8fde\u63a5\uff0c\u4f7f\u7528 mysqli_close() \u51fd\u6570\u91ca\u653e\u8d44\u6e90\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/11\/2024111100393710451.jpg\" class=\"aligncenter\" title=\"php\u600e\u4e48\u8fde\u63a5mysql\u6570\u636e\u5e93\u91cc\u7684\u6570\u636e\u63d2\u56fe\" alt=\"php\u600e\u4e48\u8fde\u63a5mysql\u6570\u636e\u5e93\u91cc\u7684\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 PHP \u8fde\u63a5 MySQL \u6570\u636e\u5e93<\/strong><\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u5efa\u7acb MySQL \u8fde\u63a5<\/strong><\/p>\n<p>\u4f7f\u7528 i_connect() \u51fd\u6570\u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\uff0c\u5b83\u9700\u8981\u56db\u4e2a\u53c2\u6570\uff1a<\/p>\n<pre>$servername = \"localhost\";\n$username = \"your_username\";\n$password = \"your_password\";\n$dbname = \"your_database_name\";\n\n$conn = mysqli_connect($servername, $username, $password, $dbname);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u8fde\u63a5\u6210\u529f\uff0c\u5c06\u8fd4\u56de\u4e00\u4e2a MySQLi \u5bf9\u8c61\u3002\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u5b83\u5c06\u8fd4\u56de false\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u6267\u884c\u67e5\u8be2<\/strong><\/p>\n<p>\u8fde\u63a5\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u6267\u884c SQL \u67e5\u8be2\u3002\u4f7f\u7528 mysqli_query() \u51fd\u6570\u5e76\u4f20\u9012 SQL \u67e5\u8be2\uff1a<\/p>\n<pre>$query = \"SELECT * FROM users\";\n$result = mysqli_query($conn, $query);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u67e5\u8be2\u6210\u529f\uff0cmysqli_query() \u5c06\u8fd4\u56de\u4e00\u4e2a MySQLi \u7ed3\u679c\u5bf9\u8c61\u3002\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u5b83\u5c06\u8fd4\u56de false\u3002<\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u5904\u7406\u7ed3\u679c<\/strong><\/p>\n<p>MySQLi \u7ed3\u679c\u5bf9\u8c61\u5305\u542b\u67e5\u8be2\u7ed3\u679c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5904\u7406\u7ed3\u679c\uff1a<\/p>\n<ul>\n<li>mysqli_fetch_assoc(): \u5c06\u7ed3\u679c\u884c\u4f5c\u4e3a\u5173\u8054\u6570\u7ec4\u8fd4\u56de\u3002<\/li>\n<li>mysqli_fetch_row(): \u5c06\u7ed3\u679c\u884c\u4f5c\u4e3a\u6570\u503c\u6570\u7ec4\u8fd4\u56de\u3002<\/li>\n<li>mysqli_fetch_object(): \u5c06\u7ed3\u679c\u884c\u4f5c\u4e3a\u5bf9\u8c61\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u8981\u5c06\u6240\u6709\u7528\u6237\u884c\u4f5c\u4e3a\u5173\u8054\u6570\u7ec4\u83b7\u53d6\uff1a<\/p>\n<pre>while ($row = mysqli_fetch_assoc($result)) {\n  \/\/ \u4f7f\u7528 $row \u6570\u7ec4...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 4\uff1a\u5173\u95ed\u8fde\u63a5<\/strong><\/p>\n<p>\u5b8c\u6210\u67e5\u8be2\u540e\uff0c\u5173\u95ed\u8fde\u63a5\u975e\u5e38\u91cd\u8981\uff1a<\/p>\n<pre>mysqli_close($conn);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fphp\u600e\u4e48\u8fde\u63a5\u6570\u636e\u5e93\u91cc\u7684\u6570\u636e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 php \u8fde\u63a5 mysql \u6570\u636e\u5e93\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528 mysqli_connect() \u51fd\u6570\u5efa\u7acb\u8fde\u63a5\u3002\u6267\u884c\u67e5\u8be2\uff0c\u4f7f\u7528 mysqli_query() \u51fd\u6570\u4f20\u9012 sql \u67e5\u8be2\u3002\u5904\u7406\u7ed3\u679c\uff0c\u4f7f\u7528 mysqli_fetch_assoc()\u3001mysqli_fetch_row() \u6216 mysqli_fetch_object() \u83b7\u53d6\u5173\u8054\u6570\u7ec4\u3001\u6570\u503c\u6570\u7ec4\u6216\u5bf9\u8c61\u5f62\u5f0f\u7684\u7ed3\u679c\u3002\u5173\u95ed\u8fde\u63a5\uff0c\u4f7f\u7528 mysqli_close() \u51fd\u6570\u91ca\u653e\u8d44\u6e90\u3002 \u5982\u4f55\u4f7f\u7528 PHP \u8fde\u63a5 MySQL \u6570\u636e\u5e93 \u6b65\u9aa4 1\uff1a\u5efa\u7acb MySQL \u8fde\u63a5 \u4f7f\u7528 i_connect() \u51fd\u6570\u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\uff0c\u5b83\u9700\u8981\u56db\u4e2a\u53c2\u6570\uff1a $servername = &#8220;localhost&#8221;; $username = &#8220;your_username&#8221;; $password = &#8220;your_password&#8221;; $dbname = &#8220;your_database_name&#8221;; $conn = mysqli_connect($servername, $username, $password, $dbname); \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u8fde\u63a5\u6210\u529f\uff0c\u5c06\u8fd4\u56de\u4e00\u4e2a MySQLi \u5bf9\u8c61\u3002\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u5b83\u5c06\u8fd4\u56de false\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-27507","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=27507"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27507\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}