{"id":160029,"date":"2024-11-03T19:41:21","date_gmt":"2024-11-03T11:41:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160029\/"},"modified":"2024-11-03T19:41:23","modified_gmt":"2024-11-03T11:41:23","slug":"mysql-%e6%95%99%e7%a8%8b-mysql-php-%e8%aa%9e%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160029\/","title":{"rendered":"MYSQL \u6559\u7a0b MySQL PHP \u8a9e\u6cd5"},"content":{"rendered":"<h1 id=\"mysql-%e6%95%99%e7%a8%8b%ef%bc%9amysql-php-%e8%aa%9e%e6%b3%95-ECvIlWrani\">MySQL \u6559\u7a0b\uff1aMySQL PHP \u8a9e\u6cd5<\/h1>\n<p>MySQL \u662f\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u5b83\u7684\u9ad8\u6548\u6027\u548c\u9748\u6d3b\u6027\u4f7f\u5176\u6210\u70ba\u958b\u767c\u8005\u7684\u9996\u9078\u3002\u672c\u6587\u5c07\u4ecb\u7d39 MySQL \u7684\u57fa\u672c\u8a9e\u6cd5\uff0c\u4e26\u5c55\u793a\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528 MySQL \u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"mysql-%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-ECvIlWrani\">MySQL \u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5728\u6df1\u5165 MySQL \u8a9e\u6cd5\u4e4b\u524d\uff0c\u4e86\u89e3\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5\u662f\u975e\u5e38\u91cd\u8981\u7684\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u5eab\uff08Database\uff09<\/strong>\uff1a\u7528\u65bc\u5b58\u5132\u6578\u64da\u7684\u5bb9\u5668\u3002<\/li>\n<li><strong>\u8868\uff08Table\uff09<\/strong>\uff1a\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u4ee5\u8868\u683c\u5f62\u5f0f\u7d44\u7e54\uff0c\u6bcf\u500b\u8868\u7531\u884c\u548c\u5217\u7d44\u6210\u3002<\/li>\n<li><strong>\u884c\uff08Row\uff09<\/strong>\uff1a\u8868\u4e2d\u7684\u4e00\u689d\u8a18\u9304\u3002<\/li>\n<li><strong>\u5217\uff08Column\uff09<\/strong>\uff1a\u8868\u4e2d\u7684\u4e00\u500b\u5c6c\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"mysql-%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-ECvIlWrani\">MySQL \u57fa\u672c\u8a9e\u6cd5<\/h2>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab-ECvIlWrani\">1. \u5275\u5efa\u6578\u64da\u5eab<\/h3>\n<pre><code>CREATE DATABASE database_name;<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab-ECvIlWrani\">2. \u4f7f\u7528\u6578\u64da\u5eab<\/h3>\n<pre><code>USE database_name;<\/code><\/pre>\n<h3 id=\"3-%e5%89%b5%e5%bb%ba%e8%a1%a8-ECvIlWrani\">3. \u5275\u5efa\u8868<\/h3>\n<pre><code>\nCREATE TABLE table_name (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100),\n    age INT\n);\n<\/code><\/pre>\n<h3 id=\"4-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-ECvIlWrani\">4. \u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>INSERT INTO table_name (name, age) VALUES ('John Doe', 30);<\/code><\/pre>\n<h3 id=\"5-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-ECvIlWrani\">5. \u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code>SELECT * FROM table_name;<\/code><\/pre>\n<h3 id=\"6-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-ECvIlWrani\">6. \u66f4\u65b0\u6578\u64da<\/h3>\n<pre><code>UPDATE table_name SET age = 31 WHERE name = 'John Doe';<\/code><\/pre>\n<h3 id=\"7-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-ECvIlWrani\">7. \u522a\u9664\u6578\u64da<\/h3>\n<pre><code>DELETE FROM table_name WHERE name = 'John Doe';<\/code><\/pre>\n<h2 id=\"%e5%9c%a8-php-%e4%b8%ad%e4%bd%bf%e7%94%a8-mysql-ECvIlWrani\">\u5728 PHP \u4e2d\u4f7f\u7528 MySQL<\/h2>\n<p>\u5728 PHP \u4e2d\u4f7f\u7528 MySQL \u9032\u884c\u6578\u64da\u64cd\u4f5c\uff0c\u901a\u5e38\u4f7f\u7528 MySQLi \u6216 PDO \u64f4\u5c55\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 MySQLi \u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-ECvIlWrani\">1. \u9023\u63a5\u5230\u6578\u64da\u5eab<\/h3>\n<pre><code>\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database_name\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\n<\/code><\/pre>\n<h3 id=\"2-%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2-ECvIlWrani\">2. \u57f7\u884c\u67e5\u8a62<\/h3>\n<pre><code>\n$sql = \"SELECT * FROM table_name\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6bcf\u884c\u6578\u64da\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"id: \" . $row[\"id\"]. \" - Name: \" . $row[\"name\"]. \" - Age: \" . $row[\"age\"]. \"<br>\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n<\/code><\/pre>\n<h3 id=\"3-%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-ECvIlWrani\">3. \u95dc\u9589\u9023\u63a5<\/h3>\n<pre><code>$conn-&gt;close();<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ECvIlWrani\">\u7e3d\u7d50<\/h2>\n<p>MySQL \u662f\u4e00\u500b\u5f37\u5927\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u80fd\u5920\u6709\u6548\u5730\u8655\u7406\u5927\u91cf\u6578\u64da\u3002\u901a\u904e\u672c\u6587\u4ecb\u7d39\u7684\u57fa\u672c\u8a9e\u6cd5\u548c PHP \u7684\u4f7f\u7528\u793a\u4f8b\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b MySQL \u7684\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u5275\u5efa\u6578\u64da\u5eab\u3001\u8868\uff0c\u9084\u662f\u9032\u884c\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\uff0cMySQL \u90fd\u63d0\u4f9b\u4e86\u7c21\u55ae\u800c\u76f4\u89c0\u7684\u8a9e\u6cd5\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 MySQL \u6578\u64da\u5eab\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f\u5176\u4ed6\u5730\u5340\u7684\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u9ad8\u6548\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2 MySQL \u6559\u7a0b\uff0c\u638c\u63e1 MySQL \u548c PHP \u8a9e\u6cd5\uff0c\u8f15\u9b06\u5efa\u7acb\u548c\u7ba1\u7406\u6578\u64da\u5eab\uff0c\u63d0\u5347\u7db2\u7ad9\u958b\u767c\u6280\u80fd\u3002<\/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-160029","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160029","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=160029"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160029\/revisions"}],"predecessor-version":[{"id":160030,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160029\/revisions\/160030"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}