visma-dev / db-class
dev-main
2023-03-31 08:58 UTC
Requires
- ext-pdo: *
This package is auto-updated.
Last update: 2024-09-30 01:43:37 UTC
README
该库实现了对值的额外验证,以使现有优秀且安全的产品更加出色,更加安全。
/** * Query Initialization * * @param string $query * @param array $parameters * @return void */ private function init(string $query, array $parameters = []) { // check the property for connection if (!$this->isConnected) { $this->connection(); } try { //Preparing the query $this->statement = $this->pdo->prepare($query); //Binding parameters $this->bind($parameters); if (!empty($this->parameters)) { foreach ($this->parameters as $param => $value) { //setting data types for our params if (is_int($value[1])) { $type = \PDO::PARAM_INT; }elseif (is_bool($value[1])) { $type = \PDO::PARAM_BOOL; }elseif (is_string($value[1])) { $type = \PDO::PARAM_STR; }else { $type = \PDO::PARAM_NULL; } $this->statement->bindValue($value[0], $value[1], $type); } } //execute the query $this->statement->execute(); }catch (\PDOException $e) { exit($e->getMessage()); } // cleaning the parameters property after execution $this->parameters = []; }
在开发此项目的过程中,我在以下方面获得了新的实际(以及理论)知识:
如何使用
- 使用此命令安装包
composer require visma-dev/db-class:@dev
- 连接autoload.php并导入类
require_once __DIR__ . '/vendor/autoload.php'; use Visma\dbClass\Database;
使用示例
选择
<?php $db = new Database([ 'host' => 'localhost', 'dbname' => 'test', 'user' => 'root', 'password' => 'root', 'charset' => 'utf8' ]); $query = $db->query('SELECT * FROM posts ORDER BY id DESC'); $db->closeConnection();
插入
<?php $insert = $db->query("INSERT INTO posts (title, content) VALUES (:title, :content)", [ 'title' => 'Hello World', 'content' => 'My name is dbClass!' ]);