├── LICENSE ├── README.md ├── composer.json └── src └── Database.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Artem Melnik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Class Database (PDO) 2 | 3 | 1. Install 4 | ```shell 5 | composer require artemmelnik/devstart-db 6 | ``` 7 | 8 | 2. Use 9 | 10 | ### Select 11 | 12 | ```php 13 | 'localhost', 21 | 'dbname' => 'devstart_db', 22 | 'user' => 'root', 23 | 'password' => 'root', 24 | 'charset' => 'utf8' 25 | ]); 26 | 27 | $query = $db->query('SELECT * FROM posts ORDER BY id DESC'); 28 | 29 | $db->closeConnection(); 30 | 31 | ``` 32 | 33 | ###Insert 34 | 35 | ```php 36 | query("INSERT INTO posts (title, content) VALUES (:title, :content)", [ 39 | 'title' => 'Hello World', 40 | 'content' => 'Wrire content' 41 | ]); 42 | 43 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "artemmelnik/devstart-db", 3 | "homepage": "https://github.com/artemmelnik/database", 4 | "authors": [ 5 | { 6 | "name": "Artem Melnik", 7 | "email": "artemmelnik989@gmail.com" 8 | } 9 | ], 10 | "autoload": { 11 | "psr-4": { 12 | "DevStart\\": "src/" 13 | } 14 | }, 15 | "require": {} 16 | } 17 | -------------------------------------------------------------------------------- /src/Database.php: -------------------------------------------------------------------------------- 1 | settings = $settings; 48 | 49 | $this->connect(); 50 | } 51 | 52 | private function connect() 53 | { 54 | $dsn = 'mysql:dbname=' . $this->settings['dbname'] . ';host=' . $this->settings['host']; 55 | 56 | try { 57 | $this->pdo = new \PDO($dsn, $this->settings['user'], $this->settings['password'], [ 58 | \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' .$this->settings['charset'] 59 | ]); 60 | 61 | # Disable emulations and we can now log 62 | $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 63 | $this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); 64 | 65 | $this->isConnected = true; 66 | 67 | } catch (\PDOException $e) { 68 | exit($e->getMessage()); 69 | } 70 | } 71 | 72 | public function closeConnection() 73 | { 74 | $this->pdo = null; 75 | } 76 | 77 | private function init(string $query, array $parameters = []) 78 | { 79 | if (!$this->isConnected) { 80 | $this->connect(); 81 | } 82 | 83 | try { 84 | # Prepare query 85 | $this->statement = $this->pdo->prepare($query); 86 | 87 | # Bind parameters 88 | $this->bind($parameters); 89 | 90 | if (!empty($this->parameters)) { 91 | foreach ($this->parameters as $param => $value) { 92 | if (is_int($value[1])) { 93 | $type = \PDO::PARAM_INT; 94 | } elseif (is_bool($value[1])) { 95 | $type = \PDO::PARAM_BOOL; 96 | } elseif (is_null($value[1])) { 97 | $type = \PDO::PARAM_NULL; 98 | } else { 99 | $type = \PDO::PARAM_STR; 100 | } 101 | 102 | $this->statement->bindValue($value[0], $value[1], $type); 103 | } 104 | } 105 | 106 | $this->statement->execute(); 107 | 108 | } catch (\PDOException $e) { 109 | exit($e->getMessage()); 110 | } 111 | 112 | $this->parameters = []; 113 | } 114 | 115 | /** 116 | * @return void 117 | * @param array $parameters 118 | */ 119 | private function bind(array $parameters): void 120 | { 121 | if (!empty($parameters) and is_array($parameters)) { 122 | $columns = array_keys($parameters); 123 | 124 | foreach ($columns as $i => &$column) { 125 | $this->parameters[sizeof($this->parameters)] = [ 126 | ':' . $column, 127 | $parameters[$column] 128 | ]; 129 | } 130 | } 131 | } 132 | 133 | /** 134 | * @param string $query 135 | * @param array $parameters 136 | * @param int $mode 137 | * @return array|int|null 138 | */ 139 | public function query(string $query, array $parameters = [], $mode = \PDO::FETCH_ASSOC) 140 | { 141 | $query = trim(str_replace('\r', '', $query)); 142 | 143 | $this->init($query, $parameters); 144 | 145 | $rawStatement = explode(' ', preg_replace("/\s+|\t+|\n+/", " ", $query)); 146 | 147 | $statement = strtolower($rawStatement[0]); 148 | 149 | if ($statement === 'select' || $statement === 'show') { 150 | return $this->statement->fetchAll($mode); 151 | } elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') { 152 | return $this->statement->rowCount(); 153 | } else { 154 | return null; 155 | } 156 | } 157 | 158 | /** 159 | * @return string 160 | */ 161 | public function lastInsertId() 162 | { 163 | return $this->pdo->lastInsertId(); 164 | } 165 | } --------------------------------------------------------------------------------