{"id":1053,"date":"2017-06-16T14:53:31","date_gmt":"2017-06-16T06:53:31","guid":{"rendered":"http:\/\/feizhaojun.com\/?p=1053"},"modified":"2017-06-16T14:55:52","modified_gmt":"2017-06-16T06:55:52","slug":"php7%e8%bf%9e%e6%8e%a5mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/feizhaojun.com\/?p=1053","title":{"rendered":"PHP7\u8fde\u63a5MySQL\u6570\u636e\u5e93\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>PHP 5 \u7684\u4f7f\u7528\u8005\u53ef\u4ee5\u4f7f\u7528 MySQL extension\uff0cmysqli \u548c PDO_MYSQL \u3002php 7\u79fb\u9664\u4e86mysql extension\uff0c\u53ea\u5269\u4e0b\u540e\u9762\u4e24\u79cd\u9009\u62e9.<br \/>\n\u8fd9\u4efd\u6587\u6863\u89e3\u91ca\u4e86\u6bcf\u4e2aAPI \u7684\u672f\u8bed\uff0c\u5e2e\u52a9\u6211\u4eec\u5982\u4f55\u4f7f\u7528API \u548c\u4e86\u89e3\u76f8\u5173API\u7684\u4fe1\u606f\u3002<br \/>\nPHP \u63d0\u4f9b\u4e86\u4e09\u79cd\u4e0d\u540c\u7684API\u53bb\u8fde\u63a5mysql\u6570\u636e\u5e93\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e863\u79cd\u4e0d\u540c\u8fde\u63a5mysql\u6570\u636e\u5e93\u7684\u65b9\u5f0f\u3002<\/p>\n<p>[code lang=\"php\"]<br \/>\n\/*<br \/>\n * mysqli<br \/>\n * \u6570\u636e\u5e93\u5730\u5740\uff0c\u767b\u9646\u8d26\u53f7\uff0c\u5bc6\u7801\uff0c\u6570\u636e\u5e93\u540d\u79f0<br \/>\n *\/<br \/>\n    $mysqli = new mysqli(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;student&quot;);<br \/>\n    $sql = &quot;SELECT * FROM tb_user&quot;;<br \/>\n    $result = $mysqli-&gt;query($sql);<br \/>\n    $row = $result-&gt;fetch_assoc(); \/\/ \u4ece\u7ed3\u679c\u96c6\u4e2d\u53d6\u5f97\u4e00\u884c\u4f5c\u4e3a\u5173\u8054\u6570\u7ec4<br \/>\n    echo $row[&quot;password&quot;];<br \/>\n    \/* free result set *\/<br \/>\n    $result-&gt;free();  <\/p>\n<p>    \/* close connection *\/<br \/>\n    $mysqli-&gt;close();  <\/p>\n<p>\/*<br \/>\n * \u7b2c\u4e00\u4e2a\u53c2\u6570\u662fmysql:host,\u7b2c\u4e8c\u662fdbname,\u7b2c\u4e09\u4e2a\u8d26\u6237\u540d\uff0c\u7b2c\u56db\u4e2a\u5bc6\u7801<br \/>\n *\/<br \/>\ntry {<br \/>\n    $pdo = new PDO(&quot;mysql:host=localhost;dbname=student&quot;, &quot;root&quot;, &quot;&quot;);<br \/>\n} catch (PDOException $e) {<br \/>\n    echo 'Connection failed: ' . $e-&gt;getMessage();<br \/>\n}<br \/>\n$sql = &quot;select * from tb_user&quot;;<br \/>\necho $sql . &quot;&lt;BR&gt;&quot;;<br \/>\n$pdo-&gt;query('set names utf8;');<br \/>\n$result = $pdo-&gt;query($sql);<br \/>\n$rows = $result-&gt;fetchAll();<br \/>\nforeach ($rows as $row) {<br \/>\n    $username = $row[1];<br \/>\n    $pwd = $row[2];<br \/>\n    echo $username;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u6211\u4eec\u63a8\u8350\u4f7f\u7528mysqli\u6216\u8005PDO_Mysql\u62d3\u5c55\uff0c\u4e0d\u63a8\u8350\u5728\u65b0\u5f00\u53d1\u4e2d\u4f7f\u7528\u8001\u7684mysql\u62d3\u5c55\uff0c\u56e0\u4e3a\u5b83\u5728PHP5.5.0\u4e2d\u4e0d\u518d\u4f7f\u7528\u5e76\u4e14\u5728PHP7.0\u4e2d\u88ab\u79fb\u9664\u3002<br \/>\n\u8bbe\u7f6e\u7f16\u7801\u5f88\u91cd\u8981,\u662futf8\u800c\u4e0d\u662fuft-8<br \/>\n[code lang=\"php\"]<br \/>\n$conn-&gt;set_charset(&quot;utf8&quot;);  <\/p>\n<p>\/\/ \u6216\u8005\u8fd9\u6837\u4e5f\u53ef\u4ee5  <\/p>\n<p>$conn-&gt;query(&quot;set names utf8;&quot;);<br \/>\n&lt;pre style=&quot;font-family: 'DejaVu Sans Mono'; font-size: 15pt; background-color: rgb(255, 255, 255);&quot;&gt;<br \/>\n[\/code]<br \/>\n\u6982\u5ff5\uff1a<br \/>\n\u6709\u7f13\u5b58\u548c\u6ca1\u6709\u7f13\u5b58\u7684\u67e5\u8be2<br \/>\n\u67e5\u8be2\u9ed8\u8ba4\u4f7f\u7528\u6709\u7f13\u5b58\u7684\u67e5\u8be2\u3002\u8fd9\u610f\u5473\u7740\u67e5\u8be2\u7ed3\u679c\u9a6c\u4e0a\u4eceMysql\u670d\u52a1\u5668\u53d1\u9001\u5230PHP\uff0c\u7136\u540e\u88ab\u4fdd\u5b58\u5728php\u89e3\u6790\u5668\u5185\u5b58\u4e2d\u3002\u8fd9\u5141\u8bb8\u989d\u5916\u7684\u64cd\u4f5c\u50cf\u8ba1\u7b97\u884c\u6570\uff0c\u79fb\u52a8\u6216\u8005\u5bfb\u627e\u5f53\u524d\u7684\u7ed3\u679c\u6307\u9488\u3002\u5b83\u4e5f\u5141\u8bb8\u5728\u540c\u6837\u7684\u8fde\u63a5\u548c\u7ed3\u679c\u96c6\u4e0a\u8fdb\u4e00\u6b65\u67e5\u8be2\u3002\u7f13\u5b58\u6a21\u5f0f\u4e0d\u597d\u7684\u4e00\u9762\u662f\u5927\u7684\u7ed3\u679c\u96c6\u53ef\u80fd\u9700\u8981\u5f88\u5927\u7684\u5185\u5b58\uff0c\u8fd9\u4e9b\u5185\u5b58\u4e00\u76f4\u5360\u7528\u76f4\u5230\u7ed3\u679c\u96c6\u88ab\u6e05\u9664\u6216\u8005\u88ab\u91ca\u653e\uff0c\u8fd9\u662f\u5728\u8bf7\u6c42\u7ed3\u675f\u81ea\u52a8\u8fdb\u884c\u7684\u3002\u672f\u8bed\u5b58\u50a8\u7ed3\u679c\u5c31\u662f\u7528\u6765\u8868\u793a\u7f13\u5b58\u6a21\u5f0f\uff0c\u6240\u6709\u7ed3\u679c\u96c6\u9a6c\u4e0a\u88ab\u4fdd\u5b58\u3002<br \/>\n\u6ca1\u6709\u7f13\u5b58\u7684Mysql\u67e5\u8be2\u88ab\u6267\u884c\uff0c\u9a6c\u4e0a\u8fd4\u56de\u4e00\u4e2a\u8d44\u6e90\uff0c\u6570\u636e\u4e00\u76f4\u7b49\u5f85mysql\u670d\u52a1\u5668\u88ab\u8fde\u63a5\u83b7\u53d6\u3002\u8fd9\u662f\u7528\u4e86\u66f4\u5c11\u7684\u5185\u5b58\u5728php\u4e00\u65b9\uff0c\u4f46\u8fd9\u56de\u589e\u52a0\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u3002\u76f4\u5230\u6240\u6709\u7684\u7ed3\u679c\u96c6\u4ece\u670d\u52a1\u5668\u88ab\u53d6\u8d70\u4e14\u6ca1\u6709\u53d1\u9001\u5efa\u7acb\u5728\u540c\u4e00\u4e2a\u8fde\u63a5\u4e0a\u7684\u67e5\u8be2\u3002\u6ca1\u6709\u7f13\u5b58\u7684\u67e5\u8be2\u4e5f\u88ab\u79f0\u4e3a\u4f7f\u7528\u7ed3\u679c\u3002<br \/>\n\u4ece\u8fd9\u4e9b\u7279\u6027\u53ef\u4ee5\u770b\u51fa\uff0c\u7f13\u5b58\u67e5\u8be2\u7528\u5728\u5f53\u4f60\u53ea\u5e0c\u671b\u5f97\u5230\u4e00\u4e2a\u6709\u9650\u7684\u7ed3\u679c\u96c6\u548c\u6216\u8005\u5728\u8bfb\u53d6\u7ed3\u679c\u96c6\u524d\u77e5\u9053\u8fd4\u56de\u7ed3\u679c\u96c6\u7684\u884c\u6570\u3002\u6ca1\u6709\u7f13\u5b58\u7684\u67e5\u8be2\u6a21\u5f0f\u7528\u5728\u5f53\u4f60\u8981\u8fd4\u56de\u5927\u91cf\u7684\u6570\u636e\u3002<br \/>\n\u56e0\u4e3a\u9ed8\u8ba4\u662f\u7f13\u5b58\u67e5\u8be2\u6a21\u5f0f\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u5c06\u4f1a\u9a8c\u8bc1\u5982\u4f55\u53bb\u6267\u884c\u6ca1\u6709\u7f13\u5b58\u7684\u67e5\u8be2API\u3002<br \/>\n[code lang=\"php\"]<br \/>\n&lt;?php<br \/>\n$mysqli  = new mysqli(&quot;localhost&quot;, &quot;my_user&quot;, &quot;my_password&quot;, &quot;world&quot;);<br \/>\n$uresult = $mysqli-&gt;query(&quot;SELECT Name FROM City&quot;, MYSQLI_USE_RESULT);  <\/p>\n<p>if ($uresult) {<br \/>\n   while ($row = $uresult-&gt;fetch_assoc()) {<br \/>\n       echo $row['Name'] . PHP_EOL;<br \/>\n   }<br \/>\n}<br \/>\n$uresult-&gt;close();<br \/>\n?&gt;<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP 5 \u7684\u4f7f\u7528\u8005\u53ef\u4ee5\u4f7f\u7528 MySQL extension\uff0cmysqli \u548c PDO_MYSQL \u3002php 7\u79fb\u9664\u4e86m&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,32,6],"tags":[],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-mysql","category-32","category-php"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1053"}],"version-history":[{"count":3,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":1056,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/1053\/revisions\/1056"}],"wp:attachment":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}