{"id":2142,"date":"2014-05-01T12:42:04","date_gmt":"2014-05-01T04:42:04","guid":{"rendered":"http:\/\/www.huzs.top\/?p=2142"},"modified":"2014-05-01T20:45:17","modified_gmt":"2014-05-01T12:45:17","slug":"php%e8%ae%bf%e9%97%aemysql%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.huzs.top\/?p=2142","title":{"rendered":"PHP\u8bbf\u95eeMySQL\u6570\u636e\u5e93\u7684\u5e38\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>PHP\u8bbf\u95eeMySQL\u6570\u636e\u5e93\u53ef\u4ee5\u4f7f\u7528\u4e09\u79cd\u63a5\u53e3:<\/p>\n<p>\u4e00.pdo_mysql(\u63a8\u8350)<\/p>\n<p>\u8fde\u63a5:<\/p>\n<pre>$dsn = \"mysql:host=localhost;dbname=test\";<br>$username = 'root';<br>$password = '123456';<br>$options = array(<br>   PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',<br>); <br>    <br>$pdo = new PDO($dsn, $username, $password, $options);<br>$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);<\/pre>\n<p>\u589e:<br \/>\u65b9\u6cd51: \u7ed1\u5b9a\u5173\u8054\u6570\u7ec4<\/p>\n<pre>$str=$pdo->prepare(\"INSERT INTO `user` (`username`, `password`) VALUES (:username,:password)\");<br>$str->execute(array(\":username\"=>\"test\", \":password\"=>\"passwd\"));<\/pre>\n<p>\u65b9\u6cd52: \u7ed1\u5b9a\u7d22\u5f15\u6570\u7ec4<\/p>\n<pre>$str=$pdo->prepare(\"INSERT INTO `user` (`username`, `password`) VALUES (?,?)\");<br>$str->execute(array(\"test\", \"passwd\"));<\/pre>\n<p>\u5220:<\/p>\n<pre>$str=$pdo->prepare(\"delete from user where id > 3\");<br>$str->execute();<\/pre>\n<p>\u6539:<\/p>\n<pre>$str=$pdo->prepare(\"UPDATE `user` SET username=:username,password=:password where id=:id\");<br>$str->execute(array(\":username\"=>\"test\", \":password\"=>\"passwd\", \":id\"=>\"3\"));<\/pre>\n<p>\u67e5:<br \/>\u65b9\u6cd51: \u5355\u4e2a\u53d6\u51fa,\u5faa\u73af\u904d\u5386,\u8fd4\u56de\u5230\u6570\u7ec4<\/p>\n<pre>$str=$pdo->prepare(\"select * from user where id > :id  order by id\");<br>$str->execute(array(\":id\"=>2));<br>$str->setFetchMode(PDO::FETCH_ASSOC);<br>\/\/\u5171\u4e09\u79cd:1.PDO::FETCH_BOTH(\u9ed8\u8ba4)  2.FETCH_ASSOC  3.FTECH_NUM<br><br>while($data=$str->fetch()){<br>\tprint_r($data);<br>\techo '<br>';<br>}<\/pre>\n<p>\u65b9\u6cd52: \u5168\u90e8\u53d6\u51fa,\u8fd4\u56de\u5230\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<pre>$str=$pdo->prepare(\"select * from user order by fid\");<br>$str->execute();<br>$data=$str->fetchAll(PDO::FETCH_NUM);<br>print_r($data);<br><\/pre>\n<p>\u65b9\u6cd53:\u5355\u4e2a\u53d6\u51fa,\u5faa\u73af\u904d\u5386,\u7ed1\u5b9a\u5b57\u6bb5\u540d\u5230\u53d8\u91cf<\/p>\n<pre>$str=$pdo->prepare(\"select fid,username,password from user order by id\");<br>$str->execute();<br><br>$str->bindColumn(\"id\",$id);<br>$str->bindColumn(\"username\",$username);<br>$str->bindColumn(3,$password);<br><br>while($str->fetch()){<br>\techo \"$id | $username | $password <br>\";<br>}<br>echo \"\u603b\u8bb0\u5f55\u6570\uff1a\".$str->rowCount().\"<br>\";<br>echo \"\u603b\u5b57\u6bb5\u6570\uff1a\".$str->columnCount().\"<br>\";<br><\/pre>\n<p>\u4e8c.mysqli<\/p>\n<pre><?php<br>\t$mysqli=new mysqli(\"localhost\", \"root\", \"123456\", \"test\");<br>\t$sql=\"select * from user  order by id\";<br>\t$result=$mysqli->query($sql);<br>\techo '<table align=\"center\" border=\"1\" width=\"800\">';<br>\twhile($row=$result->fetch_assoc()){<br>\t\techo '<tr>';<br>\t\tforeach($row as $col){<br>\t\t\techo '<td>'.$col.'<\/td>';<br>\t\t}<br>\t\techo '<\/tr>';<br>\t}<br>\techo '<\/table>';<br>\t$mysqli->close();<\/pre>\n<p>\u4e09.mysql<\/p>\n<pre><?php<br>$conn=mysql_connect(\"localhost\", \"root\", \"123456\") or die(\"\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\");<br>mysql_select_db(\"test\") or die(\"\u9009\u62e9\u6570\u636e\u5e93\u5931\u8d25\");;<br>$sql=\"select * from user\";<br>$data=mysql_query($sql);<br>echo '<table border=\"1\" align=\"center\" width=\"800\">'<br>while($row=mysql_fetch_assoc($result)){<br>\techo '<tr>';<br>\tforeach($row as $col){<br>\t\techo '<td>'.$col.'<\/td>';<br>\t}<br>\techo '<\/tr>';<br>};<br>echo '<\/table>';<br>mysql_close();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u8bbf\u95eeMySQL\u6570\u636e\u5e93\u53ef\u4ee5\u4f7f\u7528\u4e09\u79cd\u63a5\u53e3: \u4e00.pdo_mysql(\u63a8\u8350) \u8fde\u63a5: $dsn = &#8220;mysq [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2142","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts\/2142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2142"}],"version-history":[{"count":16,"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts\/2142\/revisions"}],"predecessor-version":[{"id":2158,"href":"https:\/\/www.huzs.top\/index.php?rest_route=\/wp\/v2\/posts\/2142\/revisions\/2158"}],"wp:attachment":[{"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.huzs.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}