{"id":1278,"date":"2021-05-27T21:33:41","date_gmt":"2021-05-27T13:33:41","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1278"},"modified":"2021-05-30T23:35:52","modified_gmt":"2021-05-30T15:35:52","slug":"php-mysql-%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2021\/05\/27\/php-mysql-%e5%9f%ba%e7%a1%80\/","title":{"rendered":"PHP MySQL \u57fa\u7840"},"content":{"rendered":"<p>\u901a\u8fc7PHP\uff0c\u53ef\u4ee5\u8fde\u63a5\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n<h4>MySQL<\/h4>\n<ul>\n<li>MySQL \u662f\u4e00\u79cd\u5728 Web \u4e0a\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/li>\n<li>MySQL \u662f\u4e00\u79cd\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/li>\n<li>MySQL \u4e0d\u7ba1\u5728\u5c0f\u578b\u8fd8\u662f\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u90fd\u662f\u7406\u60f3\u7684\u9009\u62e9\u3002<\/li>\n<li>MySQL \u652f\u6301\u6807\u51c6\u7684 SQL\u3002\u5728\u4e00\u4e9b\u5e73\u53f0\u4e0a\u7f16\u8bd1\u3002<\/li>\n<li>MySQL \u662f\u7531 Oracle \u516c\u53f8\u5f00\u53d1\u3001\u53d1\u5e03\u548c\u652f\u6301\u7684\u3002<\/li>\n<\/ul>\n<p><code>set xxx utf8<\/code>\u8bbe\u7f6e<code>xxx<\/code>\u8f93\u51fa\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<h4>PHP \u8fde\u63a5 MySQL<\/h4>\n<p>\u5148\u5b66\u4e60\u9762\u5411\u8fc7\u7a0b\u7684\uff0c\u56e0\u4e3a\u8981\u8003...<\/p>\n<pre><code class=\"language-mysql\">&lt;?php\n$servername = &quot;localhost&quot;;\n$username = &quot;username&quot;;\n$password = &quot;password&quot;;\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = mysqli_connect($servername, $username, $password);\n\n\/\/ \u68c0\u6d4b\u8fde\u63a5\nif (!$conn) {\n    die(&quot;Connection failed: &quot; . mysqli_connect_error());\n}\necho &quot;\u8fde\u63a5\u6210\u529f&quot;;\nmysqli_close($conn);\n?&gt;<\/code><\/pre>\n<h4>\u521b\u5efa\u6570\u636e\u5e93<\/h4>\n<p>\u5728\u6570\u636e\u5e93\u4e2d<code>CREATE DATABASE<\/code>\u547d\u4ee4\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u6570\u636e\u5e93\uff0c\u6bcf\u6761\u6570\u636e\u5e93\u547d\u4ee4\u597d\u50cf\u90fd\u9700\u8981php\u901a\u8fc7<code>query<\/code>\u51fd\u6570\u6765\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-mysql\">&lt;?php\n$servername = &quot;localhost&quot;;\n$username = &quot;username&quot;;\n$password = &quot;password&quot;;\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$db = mysqli_connect($servername, $username, $password);\n\/\/ \u68c0\u6d4b\u8fde\u63a5\nif (!$db) {\n    die(&quot;\u8fde\u63a5\u5931\u8d25: &quot; . mysqli_connect_error());\n}\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\n$sql = &quot;CREATE DATABASE zyh&quot;;\nif (mysqli_query($db, $sql)) {\n    echo &quot;\u6570\u636e\u5e93\u521b\u5efa\u6210\u529f&quot;;\n} else {\n    echo &quot;Error creating database: &quot; . mysqli_error($db);\n}\n\nmysqli_close($db);\n?&gt;\n<\/code><\/pre>\n<h4>\u521b\u5efa\u6570\u636e\u8868<\/h4>\n<p>\u521b\u5efa\u6570\u636e\u8868\u4e4b\u524d\u9700\u8981\u5148\u9009\u62e9\u6570\u636e\u5e93<code>use zyh;<\/code>\u3002\u5047\u8bbe\u901a\u8fc7\u4e0b\u9762\u6570\u636e\u5e93\u547d\u4ee4\u521b\u5efa\u6570\u636e\u8868\uff1a<\/p>\n<pre><code class=\"language-mysql\">CREATE TABLE MyGuests (\n    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n    firstname VARCHAR(30) NOT NULL,\n    lastname VARCHAR(30) NOT NULL,\n    email VARCHAR(50),\n    reg_date TIMESTAMP\n)<\/code><\/pre>\n<p><code>PRIMARY KEY<\/code>\u8868\u793a\u628a\u5b57\u6bb5<code>id<\/code>\u8bbe\u7f6e\u4e3a\u4e3b\u7801\uff0c\u5b57\u6bb5<code>id<\/code>\u4e3a\u65e0\u7b26\u53f7\u6574\u6570<code>UNSIGNED<\/code>\u7c7b\u578b\uff0c\u4e14\u6bcf\u5728\u8868\u4e2d\u65b0\u63d2\u5165\u4e00\u6761\u6570\u636e\uff0c\u6b64\u503c\u5c31\u81ea\u52a8\u52a01<code>AUTO_INCREMENT<\/code>\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u521b\u5efa\u6570\u636e\u8868\u7684php\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-php\">&lt;?php\n$servername = &quot;localhost&quot;;\n$username = &quot;username&quot;;\n$password = &quot;password&quot;;\n$dbname = &quot;zyh&quot;;\n\n$db = mysqli_connect($servername, $username, $password, $dbname);\n\/\/ \u68c0\u6d4b\u8fde\u63a5\nif (!$db) {\n    die(&quot;\u8fde\u63a5\u5931\u8d25: &quot; . mysqli_connect_error());\n}\n\n$sql = &quot;CREATE TABLE MyGuests (\nid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, \nfirstname VARCHAR(30) NOT NULL,\nlastname VARCHAR(30) NOT NULL,\nemail VARCHAR(50),\nreg_date TIMESTAMP\n)&quot;;\n\nif (mysqli_query($db, $sql)) {\n    echo &quot;\u6570\u636e\u8868 MyGuests \u521b\u5efa\u6210\u529f&quot;;\n} else {\n    echo &quot;\u521b\u5efa\u6570\u636e\u8868\u9519\u8bef: &quot; . mysqli_error($db);\n}\n\nmysqli_close($db);\n\n?&gt;<\/code><\/pre>\n<h4>\u63d2\u5165\u6570\u636e<\/h4>\n<pre><code class=\"language-php\">&lt;?php\n$servername = &quot;localhost&quot;;\n$username = &quot;username&quot;;\n$password = &quot;password&quot;;\n$dbname = &quot;zyh&quot;;\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = mysqli_connect($servername, $username, $password, $dbname);\n\/\/ \u68c0\u6d4b\u8fde\u63a5\nif (!$db) {\n    die(&quot;Connection failed: &quot; . mysqli_connect_error());\n}\n\n$sql = &quot;INSERT INTO MyGuests (firstname, lastname, email)\nVALUES (&#039;John&#039;, &#039;Doe&#039;, &#039;john@example.com&#039;)&quot;;\n\nif (mysqli_query($db, $sql)) {\n    echo &quot;\u65b0\u8bb0\u5f55\u63d2\u5165\u6210\u529f&quot;;\n} else {\n    echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . mysqli_error($db);\n}\n\nmysqli_close($db);\n?&gt;<\/code><\/pre>\n<p><code>mysqli_multi_query()<\/code> \u51fd\u6570\u53ef\u7528\u6765\u6267\u884c\u591a\u6761SQL\u8bed\u53e5\u3002\u7531\u4e8eMySQL\u672c\u6765\u5c31\u4e0d\u540c\u4e8eSQL\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u63d2\u5165\u591a\u6761\u6570\u636e\uff0c\u6240\u4ee5\u4e5f\u53ef\u4ee5\u5199\u6210\u8fd9\u79cd\u5f62\u5f0f<code>INSERT INTO TABLE_NAME () VALUES (), (), ...();<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7PHP\uff0c\u53ef\u4ee5\u8fde\u63a5\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u3002 MySQL MySQL \u662f\u4e00\u79cd\u5728 Web \u4e0a\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u3002  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1278"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=1278"}],"version-history":[{"count":7,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1278\/revisions"}],"predecessor-version":[{"id":1360,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1278\/revisions\/1360"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1278"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}