{"id":395,"date":"2017-02-05T17:53:46","date_gmt":"2017-02-05T09:53:46","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=395"},"modified":"2023-08-07T20:57:30","modified_gmt":"2023-08-07T12:57:30","slug":"php-article25","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/05\/php-article25\/","title":{"rendered":"PHP\u4e2d\u7ed3\u6784\u5316\u5206\u7ea7\u8c03\u7528mysql\u6570\u636e\u5e93"},"content":{"rendered":"<h3>\u5728php\u4e2d\u7ecf\u5e38\u4f1a\u6709\u5f88\u591a\u4e2a\u6587\u4ef6\uff0c\u5047\u5982\u5f88\u591a\u9875\u9762\u9700\u8981\u8c03\u7528\u6570\u636e\uff0c\u800c\u6bcf\u4e2a\u9875\u9762\u90fd\u5199\u4e00\u904d\u8fde\u63a5\u6570\u636e\u5e93\u7684\u4ee3\u7801\uff0c\u8fd9\u5728\u540e\u671f\u7ef4\u62a4\u7684\u65f6\u5019\u5c31\u4f1a\u7279\u522b\u9ebb\u70e6\uff0c\u5c24\u5176\u662f\u6570\u636e\u5e93\u540d\u6216\u8005\u8868\u540d\u53d1\u751f\u6539\u53d8\uff0c\u90a3\u5c31\u5f97\u4e00\u4e2a\u4e00\u4e2a\u7684\u6539\uff0c\u6240\u4ee5\u5728php\u4e2d\u64cd\u4f5cmysql\u6570\u636e\u5e93\u65f6\u6211\u662f\u5c3d\u91cf\u628a\u6570\u636e\u5e93\u8fde\u63a5\u7684\u64cd\u4f5c\u5199\u5230\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528\u65f6\u76f4\u63a5\u8c03\u7528\u76f8\u5e94\u51fd\u6570\u5373\u53ef\uff0c\u8fd9\u6837\u4f7f\u7a0b\u5e8f\u6574\u4f53\u66f4\u52a0\u7b80\u6d01\u9ad8\u6548\uff0c\u7ed3\u6784\u5316\u8bbe\u8ba1\u66f4\u5bb9\u6613\u67e5\u9605\u548c\u4fee\u6539\u3002<\/h3>\n<h3>\u4ee3\u7801\u5171\u5206\u4e09\u7ea7\uff0c\u6700\u9ad8\u7ea7database.php\uff0c\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u8fde\u63a5\u548c\u91ca\u653e\uff0c\u7136\u540e\u662ftablename_db.php,\u54cd\u5e94\u5728database\u4e2d\u8fd4\u56de\u7684\u8fde\u63a5\u503c\u4f7f\u7528sql\u8bed\u53e5\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u67e5\u8be2\uff0c\u5728\u8c03\u7528\u5173\u95ed\u51fd\u6570\uff0c\u91ca\u653e\u8fde\u63a5\u3002\u6700\u540e\u662f\u9700\u8981\u663e\u793a\u6570\u636e\u7684php\u9875\u9762\uff0c\u76f4\u63a5\u4f7f\u7528\u8c03\u53d6\u56de\u6765\u7684\u6570\u636e\u5373\u53ef\u3002\u5176\u4e2ddatabase.php\u548ctablename_db.php\u5e94\u8be5\u653e\u5728\u4e00\u4e2aconfig\u6587\u4ef6\u5939\u91cc\u3002<\/h3>\n<h3>\u4e0b\u9762\u662f\u4e09\u4e2aphp\u9875\u9762\u7684\u4ee3\u7801\uff0c\u6211\u53ea\u5199\u4e86\u67e5\u8be2\u529f\u80fd\u4e86\uff0c\u5176\u4ed6\u529f\u80fd\u76f8\u4f3c\uff0c\u81ea\u5df1\u6309\u7167\u9700\u6c42\u66f4\u6539\u5c31\u53ef\u4ee5\uff0c\u6216\u8005\u53c2\u8003\u6211\u5199\u7684\u5173\u4e8ephp\u64cd\u4f5cmysql\u6570\u636e\u5e93\u7684\u535a\u6587\uff0c\u7531\u4e8e\u8fd9\u4e09\u4e2a\u6587\u4ef6\u7684\u7f51\u5740\u4e0d\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u4fee\u6539\uff0c\u6709\u95ee\u9898\u53ef\u4ee5\u8054\u7cfb\u6211\uff0c\u4e00\u8d77\u7814\u7a76\u89e3\u51b3\u3002<\/h3>\n<h3>database.php<\/h3>\n<pre class=\"lang:php decode:true \">&lt;?php\n    class DATABASE{\n        public function conn_mysql(){   \/\/\u8fde\u63a5\u6570\u636e\u5e93\u51fd\u6570\n            $con = mysql_connect(\"localhost\",\"username\",\"password\");\/\/\u6570\u636e\u5e93\u7528\u6237\u540d\u548c\u5bc6\u7801\n            mysql_query(\"set names utf8;\");\n            if (!$con){\n                die('Could not connect: ' . mysql_error());\n            }\n            mysql_select_db(\"databasename\", $con); \/\/\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u540d\n            return $con;\n        }\n        public function close_mysql($con){  \/\/\u91ca\u653e\u8fde\u63a5\u7684\u51fd\u6570\n            mysql_close($con);\n        }\n    }\n?&gt;<\/pre>\n<h3>tablename_db.php<\/h3>\n<pre class=\"lang:php decode:true \">&lt;?php\n\/\/defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );\nrequire_once 'database.php';       \/\/\u5bfc\u5165\u4e0a\u7ea7database_db.php\u6587\u4ef6\nclass DIARY_DB{\n     public function mysql_db(){    \n        $database = new DATABASE();   \/\/\u5b9e\u4f8b\u5316\u7c7b\n        $con = $database-&gt;conn_mysql();  \/\/\u8fde\u63a5mysql\n        $result = mysql_query(\"SELECT * FROM tablename\"); \/\/\u8fdb\u884c\u67e5\u8be2\u64cd\u4f5c\n        $database-&gt;close_mysql($con);   \/\/\u91ca\u653e\u8fde\u63a5\n        return $result;  \/\/\u8fd4\u56de\u7ed3\u679c\n     }\n}\n?&gt;<\/pre>\n<h3>show.php<\/h3>\n<pre class=\"lang:php decode:true \">&lt;?php\nrequire_once 'config\/tablename_db.php';  \/\/\u5bfc\u5165\u4e0a\u7ea7tablename_db.php\u6587\u4ef6\n$article_db = new  ARTICLE_DB();   \/\/\u5b9e\u4f8b\u5316\u7c7b\n$result = $article_db-&gt;mysql_db(); \/\/\u8c03\u7528\u51fd\u6570\n$row = mysql_fetch_array($result);  \/\/\u83b7\u53d6\u67e5\u8be2\u5230\u7684\u6570\u7ec4\n?&gt;\n\/\/\u4ee5\u4e0b\u6570\u636e\u4ec5\u505a\u53c2\u8003\uff0c\u5f53\u6570\u636e\u4e0d\u6b62\u4e00\u7ec4\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528while\u8bed\u53e5\u53bb\u904d\u5386\u6570\u7ec4\n&lt;?php echo $row['a_title'];?&gt; \n&lt;?php echo $row['a_adddate'];?&gt;\n&lt;?php echo $row['a_adduser'];?&gt;\n&lt;?php echo $row['a_visit'];?&gt;\n&lt;?php echo $row['a_comment'];?&gt;\n&lt;?php echo $row['a_text'];?&gt;\n&lt;?php echo $row['a_type']; ?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728php\u4e2d\u7ecf\u5e38\u4f1a\u6709\u5f88\u591a\u4e2a\u6587\u4ef6\uff0c\u5047\u5982\u5f88\u591a\u9875\u9762\u9700\u8981\u8c03\u7528\u6570\u636e\uff0c\u800c\u6bcf\u4e2a\u9875\u9762\u90fd\u5199\u4e00\u904d\u8fde\u63a5\u6570\u636e\u5e93\u7684\u4ee3\u7801\uff0c\u8fd9\u5728\u540e\u671f\u7ef4\u62a4\u7684\u65f6\u5019\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/05\/php-article25\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":241,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[34],"tags":[56,57],"class_list":["post-395","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-php","tag-mysql","tag-php","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=395"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":1515,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/395\/revisions\/1515"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/241"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}