{"id":601,"date":"2009-08-28T12:23:00","date_gmt":"2009-08-28T04:23:00","guid":{"rendered":""},"modified":"2013-11-26T15:54:26","modified_gmt":"2013-11-26T07:54:26","slug":"php%e8%bf%9e%e6%8e%a5%e6%95%b0%e6%8d%ae%e5%ba%93%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/601.html","title":{"rendered":"PHP\u8fde\u63a5\u6570\u636e\u5e93\u7c7b"},"content":{"rendered":"<p>PHP\u8fde\u63a5\u6570\u636e\u5e93\u7c7b<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nClass MySQLDB\r\n{\r\nvar $host;\r\nvar $user;\r\nvar $passwd;\r\nvar $database;\r\nvar $conn;\r\n\/\/\u5229\u7528\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u53d8\u91cf\u521d\u59cb\u5316\r\n\/\/\u540c\u65f6\u8fde\u63a5\u6570\u636e\u5e93\u64cd\u4f5c\r\nfunction MySQLDB($host,$user,$password,$database)\r\n{\r\n$this-&gt;host = $host;\r\n$this-&gt;user = $user;\r\n$this-&gt;passwd = $password;\r\n$this-&gt;database = $database;\r\n$this-&gt;conn=mysql_connect($this-&gt;host, $this-&gt;user,$this-&gt;passwd) or die(&quot;Could not connect to $this-&gt;host&quot;);\r\nmysql_select_db($this-&gt;database,$this-&gt;conn) or die(&quot;Could not switch to database $this-&gt;database&quot;);\r\nmysql_query(&quot;set names 'gbk'&quot;);\r\n}\r\n\/\/\u8be5\u51fd\u6570\u7528\u6765\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\nfunction Close()\r\n{\r\nMySQL_close($this-&gt;conn);\r\n}\r\n\r\n\/\/\u8be5\u51fd\u6570\u5b9e\u73b0\u6570\u636e\u5e93\u67e5\u8be2\u64cd\u4f5c\r\nfunction Query($queryStr)\r\n{\r\n$res =Mysql_query($queryStr, $this-&gt;conn) or die(&quot;Could not query database&quot;);\r\nreturn $res;\r\n}\r\n\r\n\/\/\u8be5\u51fd\u6570\u8fd4\u56de\u8bb0\u5f55\u96c6\r\nfunction getRows($res)\r\n{\r\n    $rowno = 0;\r\n    $rowno = MySQL_num_rows($res);\r\n    if($rowno&gt;0)\r\n    {\r\n    for($row=0;$row&lt;$rowno;$row++ )\r\n         {\r\n            $rows&#x5B;$row]=MySQL_fetch_array($res);\r\n            \/\/\u672c\u6765\u4e3aMySQL_fetch_row,\u4f46\u662f\u4e0d\u80fd\u4ee5\u6570\u7ec4\u7684\u65b9\u5f0f\u6765\u63d0\u53d6,\u53ea\u80fd\u7528\u7d22\u5f15\r\n            \/\/\u8fd9\u6837\u53ef\u4ee5\u7528\u7d22\u5f15\u548c\u540d\u79f0\uff0c\u66f4\u4e3a\u65b9\u4fbf\r\n         }\r\n         return $rows;\r\n      }\r\n    }\r\n\r\n\/\/\u8be5\u51fd\u6570\u53d6\u56de\u6570\u636e\u5e93\u8bb0\u5f55\u6570\r\n\r\nfunction getRowsNum($res)\r\n{\r\n   $rowno = 0;\r\n   $rowno = mysql_num_rows($res);\r\n   return $rowno;\r\n}\r\n\r\n\/\/\u8be5\u51fd\u6570\u8fd4\u56de\u6570\u636e\u5e93\u8868\u5b57\u6bb5\u6570\r\nfunction getFieldsNum($res)\r\n\r\n{\r\n    $fieldno = 0;\r\n    $fieldno = mysql_num_fields($res);\r\n    return $fieldno;\r\n}\r\n\r\n\/\/\u8be5\u51fd\u6570\u8fd4\u56de\u6570\u636e\u5e93\u8868\u5b57\u6bb5\u540d\u79f0\u96c6\r\nfunction getFields($res)\r\n{\r\n$fno = $this-&gt;getFieldsNum($res);\r\n   if($fno&gt;0)\r\n      {\r\n         for($i=0;$i&lt;$fno;$i++ )\r\n         {\r\n            $fs&#x5B;$i]=MySQL_field_name($res,$i);\/\/\u53d6\u7b2ci\u4e2a\u5b57\u6bb5\u7684\u540d\u79f0\r\n         }\r\n         return $fs;\r\n      }\r\n}\r\n}\r\n<\/pre>\n<p>\u4e0b\u9762\u4e3a\u5b9e\u4f8b<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$SqlDB = new MySQLDB(&quot;127.0.0.1&quot;,&quot;root&quot;,&quot;123456&quot;,&quot;message_book&quot;);\r\n$sql = &quot;select * from ad_guestbook&quot;;\r\n$result = $SqlDB-&gt;Query($sql);\/\/\u67e5\u8be2\r\n$rs = $SqlDB-&gt;getRows($result);\/\/\u83b7\u5f97\u8bb0\u5f55\u96c6\r\n$num = $SqlDB-&gt;getRowsNum($result);\/\/\u83b7\u5f97\u8bb0\u5f55\u6570\r\n$name = $SqlDB-&gt;getFields($result);\r\n$namenum = $SqlDB-&gt;getFieldsNum($result);\r\n\r\nfor($i=0;$i&lt;$num;$i++){\r\n\r\n   echo ($rs&#x5B;$i]&#x5B;&quot;id&quot;]).&quot;    &quot; .($rs&#x5B;$i]&#x5B;&quot;typeid&quot;]).($rs&#x5B;$i]&#x5B;&quot;title&quot;]).&quot;    &quot;\r\n         .($rs&#x5B;$i]&#x5B;&quot;content&quot;]).&quot;    &quot;.($rs&#x5B;$i]&#x5B;&quot;name&quot;]).&quot;    &quot;.($rs&#x5B;$i]&#x5B;&quot;time&quot;]).&quot;    &quot;\r\n         .($rs&#x5B;$i]&#x5B;&quot;typeid&quot;]).&quot;    &quot;.($rs&#x5B;$i]&#x5B;&quot;time_1&quot;]).&quot;&lt;p&gt;&quot;;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u8fde\u63a5\u6570\u636e\u5e93\u7c7b Class MySQLDB { var $host; var $user; var $pa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-601","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=601"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions"}],"predecessor-version":[{"id":5261,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions\/5261"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}