{"id":1754,"date":"2025-02-18T00:02:33","date_gmt":"2025-02-17T16:02:33","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1754"},"modified":"2025-02-18T00:02:33","modified_gmt":"2025-02-17T16:02:33","slug":"mysql-%e8%bf%9e%e6%8e%a5%ef%bc%88%e9%99%84%e5%b8%a6%e5%ae%9e%e4%be%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/18\/mysql-%e8%bf%9e%e6%8e%a5%ef%bc%88%e9%99%84%e5%b8%a6%e5%ae%9e%e4%be%8b%ef%bc%89\/","title":{"rendered":"MySQL \u8fde\u63a5\uff08\u9644\u5e26\u5b9e\u4f8b\uff09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">MySQL \u8fde\u63a5\u662f\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u57fa\u7840\uff0cPHP \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 <strong>MySQLi<\/strong> \u548c <strong>PDO<\/strong> \u6269\u5c55\u3002\u4e0b\u9762\u6211\u5c06\u4e3a\u4f60\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u5f0f\u6765\u8fde\u63a5 MySQL\uff0c\u5e76\u9644\u5e26\u5177\u4f53\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 MySQLi \u6269\u5c55\u8fde\u63a5 MySQL<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u9762\u5411\u8fc7\u7a0b\u65b9\u5f0f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 MySQLi \u7684\u9762\u5411\u8fc7\u7a0b\u65b9\u5f0f\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u901a\u5e38\u9700\u8981\u6307\u5b9a\u4e3b\u673a\u540d\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\n$connection = mysqli_connect(\"localhost\", \"username\", \"password\", \"database_name\");\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f\nif (!$connection) {\n    die(\"Connection failed: \" . mysqli_connect_error());\n}\n\necho \"Connected successfully\";\n\n\/\/ \u5173\u95ed\u8fde\u63a5\nmysqli_close($connection);\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mysqli_connect<\/code> \u7528\u4e8e\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e3b\u673a\u5730\u5740\uff08\u901a\u5e38\u662f <code>localhost<\/code>\uff09\uff0c\u7b2c\u4e8c\u4e2a\u662f\u7528\u6237\u540d\uff0c\u7b2c\u4e09\u4e2a\u662f\u5bc6\u7801\uff0c\u7b2c\u56db\u4e2a\u662f\u6570\u636e\u5e93\u540d\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8fde\u63a5\u5931\u8d25\uff0c\u4f1a\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u7ec8\u6b62\u811a\u672c\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u9762\u5411\u5bf9\u8c61\u65b9\u5f0f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">MySQLi \u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u8fde\u63a5\u6570\u636e\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>new mysqli()<\/code> \u6765\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u521b\u5efa MySQLi \u5b9e\u4f8b\u5e76\u8fde\u63a5\u6570\u636e\u5e93\n$mysqli = new mysqli(\"localhost\", \"username\", \"password\", \"database_name\");\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f\nif ($mysqli-&gt;connect_error) {\n    die(\"Connection failed: \" . $mysqli-&gt;connect_error);\n}\n\necho \"Connected successfully\";\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$mysqli-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>new mysqli()<\/code> \u521b\u5efa\u8fde\u63a5\u5b9e\u4f8b\uff0c<code>$mysqli->connect_error<\/code> \u7528\u4e8e\u68c0\u67e5\u8fde\u63a5\u662f\u5426\u6210\u529f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 PDO \u6269\u5c55\u8fde\u63a5 MySQL<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PDO\uff08PHP Data Objects\uff09\u662f\u4e00\u4e2a\u6570\u636e\u5e93\u62bd\u8c61\u5c42\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u5305\u62ec MySQL\u3002\u4f7f\u7528 PDO \u65f6\uff0c\u8fde\u63a5\u65b9\u5f0f\u66f4\u4e3a\u7b80\u6d01\uff0c\u4e14\u652f\u6301\u9884\u5904\u7406\u8bed\u53e5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u672c\u8fde\u63a5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntry {\n    \/\/ \u521b\u5efa PDO \u5b9e\u4f8b\u5e76\u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\n    $pdo = new PDO(\"mysql:host=localhost;dbname=database_name\", \"username\", \"password\");\n\n    \/\/ \u8bbe\u7f6e PDO \u9519\u8bef\u6a21\u5f0f\u4e3a\u5f02\u5e38\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    echo \"Connected successfully\";\n} catch (PDOException $e) {\n    echo \"Connection failed: \" . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>new PDO(\"mysql:host=localhost;dbname=database_name\", \"username\", \"password\")<\/code> \u7528\u4e8e\u521b\u5efa PDO \u5b9e\u4f8b\u5e76\u8fde\u63a5 MySQL \u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><code>setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)<\/code> \u8bbe\u7f6e\u9519\u8bef\u6a21\u5f0f\u4e3a\u5f02\u5e38\uff0c\u65b9\u4fbf\u6355\u6349\u5e76\u5904\u7406\u8fde\u63a5\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6307\u5b9a\u5b57\u7b26\u96c6\uff08\u53ef\u9009\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u5e93\u8fde\u63a5\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u96c6\uff0c\u901a\u5e38\u5728\u8fde\u63a5\u65f6\u8bbe\u7f6e\u5b57\u7b26\u96c6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntry {\n    $pdo = new PDO(\"mysql:host=localhost;dbname=database_name\", \"username\", \"password\");\n    $pdo-&gt;exec(\"SET NAMES utf8\");  \/\/ \u8bbe\u7f6e\u5b57\u7b26\u96c6\u4e3a UTF-8\n\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n    echo \"Connected successfully\";\n} catch (PDOException $e) {\n    echo \"Connection failed: \" . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">PDO \u8fde\u63a5\u4f1a\u5728\u811a\u672c\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\uff0c\u56e0\u6b64\u901a\u5e38\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5173\u95ed\u8fde\u63a5\u3002\u4f60\u4e5f\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e <code>$pdo = null;<\/code> \u6765\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = null;  \/\/ \u5173\u95ed\u8fde\u63a5\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>MySQL \u8fde\u63a5\u5e38\u89c1\u9519\u8bef\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u8bba\u662f MySQLi \u8fd8\u662f PDO\uff0c\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u90fd\u53ef\u80fd\u51fa\u73b0\u4e00\u4e9b\u9519\u8bef\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u8c03\u8bd5\u548c\u5904\u7406\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>MySQLi \u9519\u8bef\u5904\u7406<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <strong>\u9762\u5411\u8fc7\u7a0b\u65b9\u5f0f<\/strong> \u4e2d\uff0c\u5982\u679c\u8fde\u63a5\u5931\u8d25\uff0c<code>mysqli_connect_error()<\/code> \u5c06\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;?php\n  $connection = mysqli_connect(\"localhost\", \"username\", \"password\", \"database_name\");\n\n  if (!$connection) {\n      die(\"Connection failed: \" . mysqli_connect_error());\n  }\n  ?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>PDO \u9519\u8bef\u5904\u7406<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <strong>try-catch<\/strong> \u5757\u6765\u6355\u83b7\u8fde\u63a5\u9519\u8bef\uff0c<code>$e->getMessage()<\/code> \u4f1a\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;?php\n  try {\n      $pdo = new PDO(\"mysql:host=localhost;dbname=database_name\", \"username\", \"password\");\n      $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n  } catch (PDOException $e) {\n      echo \"Connection failed: \" . $e-&gt;getMessage();\n  }\n  ?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQLi<\/strong> \u662f MySQL \u4e13\u7528\u7684\u6269\u5c55\uff0c\u652f\u6301\u8fc7\u7a0b\u5316\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4e0e MySQL \u6570\u636e\u5e93\u4ea4\u4e92\u3002<\/li>\n\n\n\n<li><strong>PDO<\/strong> \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u4e3a\u7edf\u4e00\u548c\u7075\u6d3b\u7684\u6570\u636e\u5e93\u63a5\u53e3\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u4e14\u63d0\u4f9b\u66f4\u597d\u7684\u5b89\u5168\u6027\uff08\u901a\u8fc7\u9884\u5904\u7406\u8bed\u53e5\u9632\u6b62 SQL \u6ce8\u5165\uff09\u3002<\/li>\n\n\n\n<li>\u4e24\u8005\u90fd\u53ef\u4ee5\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u4f46\u5982\u679c\u5c06\u6765\u53ef\u80fd\u9700\u8981\u5207\u6362\u6570\u636e\u5e93\uff08\u4f8b\u5982\u4ece MySQL \u5230 PostgreSQL\uff09\uff0c\u5219 <strong>PDO<\/strong> \u662f\u66f4\u4f18\u9009\u62e9\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e9b\u5b9e\u4f8b\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u5f0f\u8fde\u63a5\u548c\u64cd\u4f5c MySQL \u6570\u636e\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u8fde\u63a5\u662f\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u57fa\u7840\uff0cPHP \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 M [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1754","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1754"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1754\/revisions"}],"predecessor-version":[{"id":1755,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1754\/revisions\/1755"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}