{"id":1752,"date":"2025-02-17T23:59:09","date_gmt":"2025-02-17T15:59:09","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1752"},"modified":"2025-02-17T23:59:09","modified_gmt":"2025-02-17T15:59:09","slug":"mysql-php-%e8%af%ad%e6%b3%95%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\/17\/mysql-php-%e8%af%ad%e6%b3%95%ef%bc%88%e9%99%84%e5%b8%a6%e5%ae%9e%e4%be%8b%ef%bc%89\/","title":{"rendered":"MySQL PHP \u8bed\u6cd5\uff08\u9644\u5e26\u5b9e\u4f8b\uff09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">MySQL \u4e0e PHP \u7ed3\u5408\u4f7f\u7528\u975e\u5e38\u5e38\u89c1\uff0c\u5c24\u5176\u662f\u5728\u5f00\u53d1\u57fa\u4e8e\u6570\u636e\u5e93\u7684 web \u5e94\u7528\u65f6\u3002PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u4e0e MySQL \u4ea4\u4e92\uff0c\u4e3b\u8981\u5305\u62ec <code>mysqli<\/code> \u548c <code>PDO<\/code>\u3002\u4e0b\u9762\u6211\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u5f0f\u4e0e MySQL \u8fdb\u884c\u4ea4\u4e92\uff0c\u5e76\u9644\u5e26\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 MySQLi \u6269\u5c55<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>MySQLi<\/code>\uff08MySQL Improved\uff09\u6269\u5c55\u662f PHP \u4e2d\u7528\u4e8e\u4e0e MySQL \u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u7684\u63a5\u53e3\uff0c\u5b83\u652f\u6301\u9762\u5411\u5bf9\u8c61\u548c\u8fc7\u7a0b\u5316\u4e24\u79cd\u98ce\u683c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u9762\u5411\u8fc7\u7a0b\u65b9\u5f0f\n$connection = mysqli_connect(\"localhost\", \"username\", \"password\", \"database_name\");\nif (!$connection) {\n    die(\"Connection failed: \" . mysqli_connect_error());\n}\necho \"Connected successfully\";\n\n\/\/ \u9762\u5411\u5bf9\u8c61\u65b9\u5f0f\n$mysqli = new mysqli(\"localhost\", \"username\", \"password\", \"database_name\");\nif ($mysqli-&gt;connect_error) {\n    die(\"Connection failed: \" . $mysqli-&gt;connect_error);\n}\necho \"Connected successfully\";\n?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67e5\u8be2\u6570\u636e<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;?php\n  $query = \"SELECT id, name FROM users\";\n  $result = mysqli_query($connection, $query);\n\n  \/\/ \u9762\u5411\u8fc7\u7a0b\u65b9\u5f0f\n  if ($result) {\n      while ($row = mysqli_fetch_assoc($result)) {\n          echo \"ID: \" . $row&#91;\"id\"] . \" - Name: \" . $row&#91;\"name\"] . \"&lt;br&gt;\";\n      }\n  } else {\n      echo \"Error: \" . mysqli_error($connection);\n  }\n\n  \/\/ \u9762\u5411\u5bf9\u8c61\u65b9\u5f0f\n  if ($result = $mysqli-&gt;query($query)) {\n      while ($row = $result-&gt;fetch_assoc()) {\n          echo \"ID: \" . $row&#91;\"id\"] . \" - Name: \" . $row&#91;\"name\"] . \"&lt;br&gt;\";\n      }\n  } else {\n      echo \"Error: \" . $mysqli-&gt;error;\n  }\n  ?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63d2\u5165\u6570\u636e<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;?php\n  $name = \"John Doe\";\n  $query = \"INSERT INTO users (name) VALUES ('$name')\";\n  if (mysqli_query($connection, $query)) {\n      echo \"New record created successfully\";\n  } else {\n      echo \"Error: \" . mysqli_error($connection);\n  }\n  ?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u51c6\u5907\u8bed\u53e5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u51c6\u5907\u8bed\u53e5\u7528\u4e8e\u9632\u6b62 SQL \u6ce8\u5165\uff0c\u5b83\u5c06 SQL \u67e5\u8be2\u4e0e\u6570\u636e\u5206\u5f00\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$stmt = $mysqli-&gt;prepare(\"INSERT INTO users (name) VALUES (?)\");\n$stmt-&gt;bind_param(\"s\", $name);  \/\/ \"s\" stands for string\n$name = \"Jane Doe\";\n$stmt-&gt;execute();\necho \"New record created successfully\";\n$stmt-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nmysqli_close($connection);  \/\/ \u8fc7\u7a0b\u5316\u65b9\u5f0f\n$mysqli-&gt;close();  \/\/ \u9762\u5411\u5bf9\u8c61\u65b9\u5f0f\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 PDO\uff08PHP Data Objects\uff09\u6269\u5c55<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PDO \u662f\u53e6\u4e00\u79cd\u4e0e MySQL\uff08\u4ee5\u53ca\u5176\u4ed6\u6570\u636e\u5e93\uff09\u4ea4\u4e92\u7684\u65b9\u5f0f\uff0c\u652f\u6301\u66f4\u591a\u7684\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7edf\u4e00\u7684\u63a5\u53e3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h4>\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    \/\/ \u8bbe\u7f6e PDO \u9519\u8bef\u6a21\u5f0f\u4e3a\u5f02\u5e38\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>\u6267\u884c\u67e5\u8be2<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67e5\u8be2\u6570\u636e<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;?php\n  $stmt = $pdo-&gt;query(\"SELECT id, name FROM users\");\n  while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {\n      echo \"ID: \" . $row&#91;\"id\"] . \" - Name: \" . $row&#91;\"name\"] . \"&lt;br&gt;\";\n  }\n  ?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63d2\u5165\u6570\u636e<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;?php\n  $stmt = $pdo-&gt;prepare(\"INSERT INTO users (name) VALUES (:name)\");\n  $stmt-&gt;bindParam(':name', $name);\n  $name = \"John Smith\";\n  $stmt-&gt;execute();\n  echo \"New record created successfully\";\n  ?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4e8b\u52a1\u5904\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">PDO \u652f\u6301\u4e8b\u52a1\uff0c\u53ef\u4ee5\u7528\u6765\u786e\u4fdd\u591a\u6761 SQL \u8bed\u53e5\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ntry {\n    $pdo-&gt;beginTransaction();\n    $pdo-&gt;exec(\"INSERT INTO users (name) VALUES ('User 1')\");\n    $pdo-&gt;exec(\"INSERT INTO users (name) VALUES ('User 2')\");\n    $pdo-&gt;commit();\n    echo \"Transaction successful\";\n} catch (Exception $e) {\n    $pdo-&gt;rollBack();\n    echo \"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 \u4f1a\u5728\u811a\u672c\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u65e0\u9700\u663e\u5f0f\u5173\u95ed\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5e38\u89c1\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQLi \u9519\u8bef\u5904\u7406<\/strong>\uff1a<\/li>\n\n\n\n<li>\u8fc7\u7a0b\u5316\u65b9\u5f0f\uff1a<br><code>php if (!$result) { echo \"Error: \" . mysqli_error($connection); }<\/code><\/li>\n\n\n\n<li>\u9762\u5411\u5bf9\u8c61\u65b9\u5f0f\uff1a <code>if (!$result) { echo \"Error: \" . $mysqli->error; }<\/code><\/li>\n\n\n\n<li><strong>PDO \u9519\u8bef\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>PDO::ATTR_ERRMODE<\/code> \u5c5e\u6027\u6765\u542f\u7528\u5f02\u5e38\u6a21\u5f0f\uff0c\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  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  }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3\u4e0e\u9009\u62e9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQLi<\/strong> \u9002\u7528\u4e8e\u53ea\u9700\u8981\u4e0e MySQL \u6570\u636e\u5e93\u4ea4\u4e92\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u652f\u6301\u8fc7\u7a0b\u5316\u548c\u9762\u5411\u5bf9\u8c61\u4e24\u79cd\u98ce\u683c\u3002<\/li>\n\n\n\n<li><strong>PDO<\/strong> \u662f\u66f4\u7075\u6d3b\u4e14\u53ef\u6269\u5c55\u7684\u9009\u9879\uff0c\u5b83\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff08MySQL\u3001PostgreSQL\u3001SQLite \u7b49\uff09\uff0c\u5e76\u63d0\u4f9b\u66f4\u4e00\u81f4\u7684\u63a5\u53e3\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u6269\u5c55\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u9700\u6c42\u3001\u6570\u636e\u5e93\u7c7b\u578b\u4ee5\u53ca\u5f00\u53d1\u8005\u7684\u4e2a\u4eba\u504f\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u4e0e PHP \u7ed3\u5408\u4f7f\u7528\u975e\u5e38\u5e38\u89c1\uff0c\u5c24\u5176\u662f\u5728\u5f00\u53d1\u57fa\u4e8e\u6570\u636e\u5e93\u7684 web \u5e94\u7528\u65f6\u3002PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u4e0e  [&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-1752","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1752","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=1752"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1752\/revisions"}],"predecessor-version":[{"id":1753,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1752\/revisions\/1753"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}