{"id":2211,"date":"2023-03-31T16:21:39","date_gmt":"2023-03-31T08:21:39","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2211"},"modified":"2023-03-31T16:21:39","modified_gmt":"2023-03-31T08:21:39","slug":"2211","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2211.html","title":{"rendered":"php\u5b9e\u73b0pdo\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\u8fc7\u7a0b\u8be6\u89e3"},"content":{"rendered":"
\u4f18\u70b9\u53ef\u4ee5\u8bf4\u662f\u975e\u5e38\u591a\u4e86\uff0c\u5e38\u89c1\u7684\u4f18\u70b9\u5c31\u662f\u4fbf\u4e8e\u7ef4\u62a4\u3001\u590d\u7528\u3001\u9ad8\u6548\u3001\u5b89\u5168\u3001\u6613\u6269\u5c55\u3002\u4f8b\u5982PDO\u652f\u6301\u7684\u6570\u636e\u5e93\u7c7b\u578b\u662f\u975e\u5e38\u591a\u7684\uff0c\u4e0emysqli\u4e0d\u540c\u7684\u5c31\u662f\uff0cPDO\u8fd8\u652f\u6301\u5176\u4ed6\u6570\u636e\u5e93\uff0c\u4e00\u5957\u5199\u6cd5\u591a\u79cd\u6570\u636e\u5e93\u7684\u5339\u914d\uff0c\u800cmysqli\u4ec5\u4ec5\u662f\u652f\u6301mysql\u3002<\/span><\/p>\n Db.php<\/span><\/p>\n Demo.php<\/span><\/p>\n <\/p>\n \u5168\u9009\u4e0b\u9762\u7684SQL\u8bed\u53e5\u7c98\u8d34\u81f3\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\u7684SQL\u6267\u884c\u6846\u8fdb\u884c\u6267\u884c\u5373\u53ef\u5feb\u901f\u521b\u5efa\u6d4b\u8bd5\u6570\u636e\u5e93\u3002<\/span><\/p>\n \u5230\u6b64\u8fd9\u7bc7\u5173\u4e8ephp\u5b9e\u73b0pdo\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\u8fc7\u7a0b\u8be6\u89e3<\/span><\/p>\n","protected":false},"excerpt":{"rendered":" \u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\u7684\u4f18\u70b9 \u4f18\u70b9\u53ef\u4ee5\u8bf4\u662f\u975e\u5e38\u591a\u4e86\uff0c\u5e38\u89c1\u7684\u4f18\u70b9\u5c31\u662f\u4fbf\u4e8e\u7ef4\u62a4\u3001\u590d\u7528\u3001\u9ad8\u6548\u3001\u5b89\u5168\u3001\u6613\u6269\u5c55\u3002\u4f8b\u5982PDO\u652f\u6301\u7684\u6570 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-2211","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2211"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2211"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2211\/revisions"}],"predecessor-version":[{"id":2212,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2211\/revisions\/2212"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\u4ee3\u7801<\/h2>\n
<?php\r\n\/**\r\n * \u6570\u636e\u5e93\u7684\u57fa\u672c\u64cd\u4f5c\r\n *\/\r\nclass Db\r\n{\r\n \/\/ \u6570\u636e\u5e93\u7684\u9ed8\u8ba4\u8fde\u63a5\u53c2\u6570\r\n private $dbConfig=[\r\n 'db'=>'mysql', \/\/ \u6570\u636e\u5e93\u7c7b\u578b\r\n 'host'=>'localhost', \/\/ \u4e3b\u673a\u540d\u79f0\r\n 'port'=>'3306', \/\/ \u9ed8\u8ba4\u7aef\u53e3\r\n 'user'=>'root', \/\/ \u7528\u6237\u540d\r\n 'pass'=>'root', \/\/ \u5bc6\u7801\r\n 'charset'=>'utf8', \/\/ \u9ed8\u8ba4\u5b57\u7b26\u96c6\r\n 'dbname'=>'edu', \/\/ \u9ed8\u8ba4\u6570\u636e\u5e93\r\n ];\r\n \/\/ \u65b0\u589e\u4e3b\u952eid\r\n public $insertId = null;\r\n \/\/ \u53d7\u5f71\u54cd\u7684\u8bb0\u5f55\r\n public $num = 0;\r\n \/\/ \u5355\u4f8b\u6a21\u5f0f\uff0c\u672c\u7c7b\u7684\u5b9e\u4f8b\r\n private static $instance = null;\r\n \/\/ \u6570\u636e\u5e93\u7684\u8fde\u63a5\r\n private $conn = null;\r\n \/**\r\n * Db\u6784\u9020\u65b9\u6cd5\r\n * \u79c1\u6709\u5316\u4ee5\u9632\u6b62\u5916\u90e8\u5b9e\u4f8b\u5316\r\n *\/\r\n private function __construct($params=[])\r\n {\r\n \/\/ \u521d\u59cb\u5316\u8fde\u63a5\u53c2\u6570\r\n $this->dbConfig = array_merge($this->dbConfig,$params);\r\n \/\/ \u8fde\u63a5\u6570\u636e\u5e93\r\n $this->connect();\r\n }\r\n \/**\r\n * \u7981\u6b62\u5916\u90e8\u514b\u9686\u8be5\u5b9e\u4f8b\r\n *\/\r\n private function __clone()\r\n {\r\n \/\/ TODO\uff1aImplement __clone() method.\r\n }\r\n \/**\r\n * \u83b7\u53d6\u5f53\u524d\u7c7b\u7684\u5355\u4e00\u5b9e\u4f8b\r\n *\/\r\n public static function getInstance($params=[])\r\n {\r\n if (!self::$instance instanceof self) {\r\n self::$instance = new self($params);\r\n }\r\n return self::$instance;\r\n }\r\n private function connect()\r\n {\r\n try{\r\n \/\/ \u914d\u7f6e\u6570\u636e\u6e90DSN\r\n $dsn = "{$this->dbConfig['db']}:host={$this->dbConfig['host']};port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']};charset={$this->dbConfig['charset']}";\r\n \/\/ \u521b\u5efaPDO\u5bf9\u8c61\r\n $this->conn = new PDO($dsn,$this->dbConfig['user'],$this->dbConfig['pass']);\r\n \/\/ \u8bbe\u7f6e\u5ba2\u6237\u7aef\u5b57\u7b26\u96c6\r\n $this->conn->query("SET NAMES {$this->dbConfig['charset']}");\r\n }catch (PDOException $e){\r\n die('\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25'.$e->getMessage());\r\n }\r\n }\r\n \/** \r\n * \u5b8c\u6210\u6570\u636e\u8868\u7684\u64cd\u4f5c\uff1aCURD\r\n *\/\r\n public function exec($sql)\r\n {\r\n $num = $this->conn->exec($sql);\r\n \/\/ \u5982\u679c\u6709\u53d7\u5f71\u54cd\u7684\u8bb0\u5f55\r\n if($num > 0){\r\n \/\/ \u5982\u679c\u662f\u65b0\u589e\u64cd\u4f5c\uff0c\u521d\u59cb\u5316\u65b0\u589e\u4e3b\u952eid\u5c5e\u6027\r\n if(null !==$this->conn->lastInsertId()){\r\n $this->insertId = $this->conn->lastInsertId();\r\n }\r\n $this->num = $num; \/\/ \u8fd4\u56de\u53d7\u5f71\u54cd\u7684\u8bb0\u5f55\r\n }else{\r\n $error = $this->conn->errorInfo(); \/\/ \u83b7\u53d6\u6700\u540e\u64cd\u4f5c\u9519\u8bef\u4fe1\u606f\u7684\u6570\u7ec4\r\n var_dump($error);\r\n }\r\n }\r\n \/\/ \u83b7\u53d6\u5355\u6761\u67e5\u8be2\u7ed3\u679c\r\n public function fetch($sql)\r\n {\r\n return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);\r\n }\r\n \/\/ \u83b7\u53d6\u591a\u6761\u67e5\u8be2\u7ed3\u679c\r\n public function fetchAll($sql)\r\n {\r\n return $this->conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);\r\n }\r\n}\r\n?><\/pre>\n
\u8c03\u7528<\/h2>\n
<?php\r\n\/**\r\n * Db\u7c7b\u6d4b\u8bd5\r\n *\/\r\nrequire 'Db.php';\r\n\/\/ \u83b7\u53d6Db\u7c7b\u5b9e\u4f8b\r\n$db = Db::getInstance();\r\n\/\/ \u65b0\u589e\u64cd\u4f5c\r\n\/\/ $sql = "INSERT student SET name='\u5f20\u4e00\u9e23',email='ByteDance@qq.com',grade='59',course='golang'";\r\n\/\/ $db->exec($sql);\r\n\/\/ echo '\u6210\u529f\u63d2\u5165\u4e86'.$db->num.'\u6761\u8bb0\u5f55\uff0c\u4e3b\u952eid\u662f'.$db->insertId;\r\n\/\/ \u5220\u9664\u64cd\u4f5c\r\n\/\/ $sql = "DELETE FROM student WHERE id='4'";\r\n\/\/ $db->exec($sql);\r\n\/\/ echo '\u6210\u529f\u5220\u9664\u4e86'.$db->num.'\u6761\u8bb0\u5f55';\r\n\/\/ \u66f4\u65b0\u64cd\u4f5c\r\n\/\/ $sql = "UPDATE student SET grade='1199' WHERE id='1'";\r\n\/\/ $db->exec($sql);\r\n\/\/ echo '\u6210\u529f\u66f4\u65b0\u4e86'.$db->num.'\u6761\u8bb0\u5f55';\r\n\/\/ \u67e5\u8be2\u5355\u6761\u64cd\u4f5c\r\n\/\/ $sql = "SELECT id,name,email,grade FROM student WHERE grade < '60'";\r\n\/\/ $row = $db->fetch($sql);\r\n\/\/ var_dump($row);\r\n\/\/ \/\/ \u67e5\u8be2\u591a\u6761\u64cd\u4f5c\r\n\/\/ $sql = "SELECT id,name,email,grade FROM student WHERE grade > '80'";\r\n\/\/ $rows = $db->fetchAll($sql);\r\n\/\/ var_dump($rows);\r\n?><\/pre>\n
\u6570\u636e\u5e93\u8868\u683c\u7ed3\u6784<\/h2>\n
SQL<\/h2>\n
-- phpMyAdmin SQL Dump\r\n-- version 4.8.5\r\n-- https:\/\/www.phpmyadmin.net\/\r\n--\r\n-- \u4e3b\u673a\uff1a localhost\r\n-- \u751f\u6210\u65e5\u671f\uff1a 2022-08-08 10:46:35\r\n-- \u670d\u52a1\u5668\u7248\u672c\uff1a 5.7.26\r\n-- PHP \u7248\u672c\uff1a 7.3.4\r\nSET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";\r\nSET AUTOCOMMIT = 0;\r\nSTART TRANSACTION;\r\nSET time_zone = "+00:00";\r\n\/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT *\/;\r\n\/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS *\/;\r\n\/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION *\/;\r\n\/*!40101 SET NAMES utf8mb4 *\/;\r\n--\r\n-- \u6570\u636e\u5e93\uff1a `edu`\r\n--\r\n-- --------------------------------------------------------\r\n--\r\n-- \u8868\u7684\u7ed3\u6784 `student`\r\n--\r\nCREATE TABLE `student` (\r\n `id` int(11) NOT NULL,\r\n `name` varchar(32) COLLATE utf8_unicode_ci NOT NULL,\r\n `email` text COLLATE utf8_unicode_ci NOT NULL,\r\n `grade` varchar(32) COLLATE utf8_unicode_ci NOT NULL,\r\n `course` varchar(32) COLLATE utf8_unicode_ci NOT NULL\r\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\r\n--\r\n-- \u8f6c\u5b58\u8868\u4e2d\u7684\u6570\u636e `student`\r\n--\r\nINSERT INTO `student` (`id`, `name`, `email`, `grade`, `course`) VALUES\r\n(1, '\u738b\u5174', 'meituan@qq.com', '80', 'ruby'),\r\n(2, '\u9ec4\u5ce5', 'pdd@qq.com', '68', 'mysql'),\r\n(6, '\u674e\u5f66\u5b8f', 'baidu@qq.com', '95', 'python'),\r\n(5, '\u9a6c\u4e91', 'mayun@qq.com', '88', 'php'),\r\n(7, '\u5218\u5f3a\u4e1c', 'jd@qq.com', '76', 'C++'),\r\n(8, '\u9a6c\u5316\u817e', '10001@qq.com', '59', 'java'),\r\n(9, '\u5f20\u4e00\u9e23', 'ByteDance@qq.com', '77', 'golang');\r\n--\r\n-- \u8f6c\u50a8\u8868\u7684\u7d22\u5f15\r\n--\r\n--\r\n-- \u8868\u7684\u7d22\u5f15 `student`\r\n--\r\nALTER TABLE `student`\r\n ADD PRIMARY KEY (`id`);\r\n--\r\n-- \u5728\u5bfc\u51fa\u7684\u8868\u4f7f\u7528AUTO_INCREMENT\r\n--\r\n--\r\n-- \u4f7f\u7528\u8868AUTO_INCREMENT `student`\r\n--\r\nALTER TABLE `student`\r\n MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;\r\nCOMMIT;\r\n\/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT *\/;\r\n\/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS *\/;\r\n\/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION *\/;<\/pre>\n