visma-dev / db-class
dev-main
2023-03-31 08:58 UTC
Requires
- ext-pdo: *
This package is auto-updated.
Last update: 2025-07-29 02:40:55 UTC
README
The library implements additional validation of values in order to make an already good and safe product even better and even safer.
/** * 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 = []; }
While working on this project, I gained new practical (as well as theoretical) knowledge in working with:
How to use:
- Use this command to install the package
composer require visma-dev/db-class:@dev
- Connect the autoload.php and import the class
require_once __DIR__ . '/vendor/autoload.php'; use Visma\dbClass\Database;
Usage examples
Select
<?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();
Insert
<?php $insert = $db->query("INSERT INTO posts (title, content) VALUES (:title, :content)", [ 'title' => 'Hello World', 'content' => 'My name is dbClass!' ]);