{"id":3084,"date":"2025-03-22T00:00:37","date_gmt":"2025-03-21T16:00:37","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3084"},"modified":"2025-03-22T00:00:37","modified_gmt":"2025-03-21T16:00:37","slug":"php-mysql-%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/22\/php-mysql-%e7%ae%80%e4%bb%8b\/","title":{"rendered":"PHP MySQL \u7b80\u4ecb"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. PHP \u4e0e MySQL \u7684\u5173\u7cfb<\/h2>\n\n\n\n<p>PHP\uff08Hypertext Preprocessor\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e Web \u5f00\u53d1\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\uff0c\u800c MySQL \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff08RDBMS\uff09\u3002PHP \u4e0e MySQL \u7684\u7ed3\u5408\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u6784\u5efa\u52a8\u6001\u7684\u6570\u636e\u5e93\u9a71\u52a8\u578b Web \u5e94\u7528\uff0c\u5982\u535a\u5ba2\u3001CMS\u3001\u7535\u5b50\u5546\u52a1\u7f51\u7ad9\u7b49\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. PHP \u548c MySQL \u7684\u7ed3\u5408\u65b9\u5f0f<\/h2>\n\n\n\n<p>PHP \u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4e0e MySQL \u4ea4\u4e92\uff0c\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQLi\uff08MySQL Improved\uff09<\/strong>\uff1a\u652f\u6301\u9762\u5411\u5bf9\u8c61\u548c\u8fc7\u7a0b\u5316\u4e24\u79cd\u98ce\u683c\uff0c\u652f\u6301\u9884\u5904\u7406\u8bed\u53e5\uff0c\u63a8\u8350\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>PDO\uff08PHP Data Objects\uff09<\/strong>\uff1a\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7ea7\u7684\u6570\u636e\u5e93\u8bbf\u95ee\u63a5\u53e3\u3002<\/li>\n\n\n\n<li><strong>\u8001\u7248 MySQL \u6269\u5c55\uff08\u5df2\u5e9f\u5f03\uff09<\/strong>\uff1a\u4ece PHP 5.5 \u5f00\u59cb\u5e9f\u5f03\uff0cPHP 7 \u53ca\u66f4\u9ad8\u7248\u672c\u4e0d\u518d\u652f\u6301\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. PHP \u8fde\u63a5 MySQL \u6570\u636e\u5e93<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u4f7f\u7528 MySQLi \u8fde\u63a5\u6570\u636e\u5e93\uff08\u9762\u5411\u8fc7\u7a0b\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$database = \"testdb\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = mysqli_connect($servername, $username, $password, $database);\n\n\/\/ \u68c0\u6d4b\u8fde\u63a5\nif (!$conn) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . mysqli_connect_error());\n}\necho \"\u8fde\u63a5\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u4f7f\u7528 MySQLi \u8fde\u63a5\u6570\u636e\u5e93\uff08\u9762\u5411\u5bf9\u8c61\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$database = \"testdb\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $database);\n\n\/\/ \u68c0\u6d4b\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\necho \"\u8fde\u63a5\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u4f7f\u7528 PDO \u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$dsn = \"mysql:host=localhost;dbname=testdb;charset=utf8mb4\";\n$username = \"root\";\n$password = \"\";\n\ntry {\n    $pdo = new PDO($dsn, $username, $password, &#91;\n        PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION\n    ]);\n    echo \"\u8fde\u63a5\u6210\u529f\";\n} catch (PDOException $e) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $e-&gt;getMessage());\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. PHP \u6267\u884c MySQL \u67e5\u8be2<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 MySQLi \u67e5\u8be2\uff08\u8fc7\u7a0b\u5316\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"SELECT id, name FROM users\";\n$result = mysqli_query($conn, $sql);\n\nif (mysqli_num_rows($result) &gt; 0) {\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 \"0 \u7ed3\u679c\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 PDO \u67e5\u8be2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"SELECT id, name FROM users\";\n$stmt = $pdo-&gt;query($sql);\n\nwhile ($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<h2 class=\"wp-block-heading\">5. PHP MySQL \u63d2\u5165\u6570\u636e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 MySQLi \u63d2\u5165\u6570\u636e\uff08\u8fc7\u7a0b\u5316\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"INSERT INTO users (name, email) VALUES ('\u5f20\u4e09', 'zhangsan@example.com')\";\nif (mysqli_query($conn, $sql)) {\n    echo \"\u6570\u636e\u63d2\u5165\u6210\u529f\";\n} else {\n    echo \"\u9519\u8bef: \" . mysqli_error($conn);\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 PDO \u63d2\u5165\u6570\u636e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"INSERT INTO users (name, email) VALUES (:name, :email)\";\n$stmt = $pdo-&gt;prepare($sql);\n$stmt-&gt;execute(&#91;\n    ':name' =&gt; '\u5f20\u4e09',\n    ':email' =&gt; 'zhangsan@example.com'\n]);\necho \"\u6570\u636e\u63d2\u5165\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. PHP \u9884\u5904\u7406\u8bed\u53e5\uff08\u9632\u6b62 SQL \u6ce8\u5165\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 MySQLi \u9884\u5904\u7406\u8bed\u53e5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"INSERT INTO users (name, email) VALUES (?, ?)\";\n$stmt = mysqli_prepare($conn, $sql);\nmysqli_stmt_bind_param($stmt, \"ss\", $name, $email);\n\n$name = \"\u674e\u56db\";\n$email = \"lisi@example.com\";\nmysqli_stmt_execute($stmt);\n\necho \"\u6570\u636e\u63d2\u5165\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 PDO \u9884\u5904\u7406\u8bed\u53e5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"INSERT INTO users (name, email) VALUES (:name, :email)\";\n$stmt = $pdo-&gt;prepare($sql);\n$stmt-&gt;execute(&#91;\n    ':name' =&gt; '\u674e\u56db',\n    ':email' =&gt; 'lisi@example.com'\n]);\necho \"\u6570\u636e\u63d2\u5165\u6210\u529f\";\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. PHP MySQL \u66f4\u65b0\u6570\u636e<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"UPDATE users SET email = 'newemail@example.com' WHERE name = '\u5f20\u4e09'\";\nif (mysqli_query($conn, $sql)) {\n    echo \"\u6570\u636e\u66f4\u65b0\u6210\u529f\";\n} else {\n    echo \"\u9519\u8bef: \" . mysqli_error($conn);\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">8. PHP MySQL \u5220\u9664\u6570\u636e<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$sql = \"DELETE FROM users WHERE name = '\u674e\u56db'\";\nif (mysqli_query($conn, $sql)) {\n    echo \"\u6570\u636e\u5220\u9664\u6210\u529f\";\n} else {\n    echo \"\u9519\u8bef: \" . mysqli_error($conn);\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">9. \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nmysqli_close($conn); \/\/ MySQLi \u5173\u95ed\u8fde\u63a5\n$pdo = null; \/\/ PDO \u5173\u95ed\u8fde\u63a5\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. \u76f8\u5173\u94fe\u63a5\uff08\u6df1\u5165\u5b66\u4e60\uff09<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP \u5b98\u65b9\u6587\u6863\uff08https:\/\/www.php.net\/manual\/\uff09<\/li>\n\n\n\n<li>MySQL \u5b98\u65b9\u6587\u6863\uff08https:\/\/dev.mysql.com\/doc\/\uff09<\/li>\n\n\n\n<li>PHP MySQLi \u6559\u7a0b\uff08https:\/\/www.w3schools.com\/php\/php_mysql_intro.asp\uff09<\/li>\n\n\n\n<li>PHP PDO \u6559\u7a0b\uff08https:\/\/www.php.net\/manual\/en\/book.pdo.php\uff09<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">11. \u603b\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQLi<\/strong> \u9002\u7528\u4e8e MySQL \u6570\u636e\u5e93\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> \u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u63a8\u8350\u4f7f\u7528\uff0c\u5c24\u5176\u9002\u5408\u6709\u591a\u4e2a\u6570\u636e\u5e93\u9700\u6c42\u7684\u9879\u76ee\u3002<\/li>\n\n\n\n<li><strong>\u9884\u5904\u7406\u8bed\u53e5<\/strong> \u53ef\u6709\u6548\u9632\u6b62 SQL \u6ce8\u5165\uff0c\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/li>\n\n\n\n<li>PHP \u4e0e MySQL \u7ed3\u5408\u540e\uff0c\u53ef\u4ee5\u8f7b\u677e\u5f00\u53d1\u52a8\u6001\u6570\u636e\u9a71\u52a8\u7684\u7f51\u7ad9\uff0c\u5982 CMS\u3001\u535a\u5ba2\u3001\u7535\u5546\u5e73\u53f0\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. PHP \u4e0e MySQL \u7684\u5173\u7cfb PHP\uff08Hypertext Preprocessor\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e W [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3085,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3084","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3084","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=3084"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3084\/revisions"}],"predecessor-version":[{"id":3086,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3084\/revisions\/3086"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3085"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}