Last active
October 25, 2018 03:10
-
-
Save flxxyz/9556462dbb90d6ae527464504f90c697 to your computer and use it in GitHub Desktop.
简单封装的PDO操作类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE `feedback` ( | |
| `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, | |
| `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户id', | |
| `platform` varchar(64) NOT NULL DEFAULT '' COMMENT '平台', | |
| `os` varchar(64) NOT NULL DEFAULT '' COMMENT '系统版本', | |
| `device` varchar(64) NOT NULL DEFAULT '' COMMENT '设备名称', | |
| `account_type` varchar(64) NOT NULL DEFAULT '' COMMENT '用户类型 facebook,guest', | |
| `issue` varchar(255) NOT NULL DEFAULT 0 COMMENT '反馈问题', | |
| `content` text NOT NULL COMMENT '提交的反馈内容', | |
| `email` varchar(255) NOT NULL DEFAULT '' COMMENT '邮箱地址', | |
| PRIMARY KEY (`id`), | |
| index IK_user_id (`user_id`), | |
| index IK_platform (`platform`), | |
| index IK_os (`os`), | |
| index IK_device (`device`), | |
| index IK_account_type (`account_type`), | |
| index IK_issue (`issue`), | |
| index IK_email (`email`) | |
| ) engine=InnoDB DEFAULT CHARSET=utf8 COMMENT='反馈表'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Class Db | |
| */ | |
| class Db | |
| { | |
| private static $instance = null; | |
| private $host = '127.0.0.1'; | |
| private $port = '3306'; | |
| private $user = 'root'; | |
| private $pass = '12345678'; | |
| private $dbname = 'test'; | |
| private $conn; | |
| private function __construct() | |
| { | |
| //$this->host = $hostname; | |
| //$this->user = $username; | |
| //$this->pass = $password; | |
| //$this->dbname = $dbname; | |
| $opts = [ | |
| \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, | |
| \PDO::ATTR_EMULATE_PREPARES => false, | |
| ]; | |
| try { | |
| $this->conn = | |
| new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname | |
| .';port='.$this->port.';charset=UTF8', $this->user, | |
| $this->pass, $opts); | |
| $this->conn->setAttribute(\PDO::ATTR_ERRMODE, | |
| \PDO::ERRMODE_EXCEPTION); | |
| $this->conn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); | |
| } catch (\PDOException $e) { | |
| echo $e->getMessage(); | |
| } | |
| } | |
| public static function getInstance() | |
| { | |
| if (!isset(self::$instance)) { | |
| self::$instance = new self(); | |
| } | |
| return self::$instance; | |
| } | |
| public function getConnection() | |
| { | |
| return $this->conn; | |
| } | |
| public function insert($sql, $params = [], $return = false) | |
| { | |
| try { | |
| $stmt = $this->conn->prepare($sql); | |
| if (!empty($stmt)) { | |
| foreach ($params as $param) { | |
| if ($param['type'] == 'int') { | |
| $stmt->bindParam($param['name'], $param['value'], | |
| PDO::PARAM_INT); | |
| } else { | |
| $stmt->bindParam($param['name'], $param['value'], | |
| PDO::PARAM_STR); | |
| } | |
| } | |
| } | |
| $stmt->execute(); | |
| if ($return == true) { | |
| return $this->conn->lastInsertId(); | |
| } | |
| return true; | |
| } catch (\PDOException $e) { | |
| echo $e->getMessage(); | |
| } | |
| } | |
| private function __clone() | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // @link https://gist.github.com/flxxyz/b11c491061a59aafc9bf6a688457cac0#file-log-php | |
| require_once __DIR__.'/Log.php'; | |
| require_once __DIR__.'/Db.php'; | |
| $request = $_REQUEST; | |
| $issue = isset($request['issue']) ? $request['issue'] : ''; | |
| $content = isset($request['content']) ? $request['content'] : ''; | |
| $email = isset($request['email']) ? $request['email'] : ''; | |
| $params = [ | |
| ['name' => ':issue', 'value' => $issue, 'type' => 'string'], | |
| ['name' => ':content', 'value' => $content, 'type' => 'string'], | |
| ['name' => ':email', 'value' => $email, 'type' => 'string'], | |
| ]; | |
| $sql = | |
| "INSERT INTO feedback (issue,content,email) values (:issue,:content,:email)"; | |
| $insert_id = DB::getInstance()->insert($sql, $params, true); | |
| Log::notice('return insert_id is '.$insert_id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment