Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
riculum committed Apr 15, 2022
1 parent 6a2cae0 commit 0d9c524
Show file tree
Hide file tree
Showing 6 changed files with 750 additions and 2 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Riculum
Copyright (c) 2021 Riculum

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
119 changes: 118 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,118 @@
# PHP-PDO
# PHP-PDO
A complete database toolkit written in PHP to handle PDO statements

## Installation
Use the package manager [composer](https://getcomposer.org) to install the library
```bash
composer require riculum/php-pdo
```

## Initial setup

### Credentials
The basic database settings can be set through environment variables. Add a `.env` file in the root of your project. Make sure the `.env` file is added to your `.gitignore` so it is not checked-in the code. By default, the library looks for the following variables:

* DB_HOST
* DB_NAME
* DB_USERNAME
* DB_PASSWORD

More information how to use environment variables [here](https://github.com/vlucas/phpdotenv)

### Configuration
Import vendor/autoload.php and load the `.env` settings
```php
require_once 'vendor/autoload.php';

use Database\Core\Database as DB;

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
```

## Statements
The `select` statement is used to return an array containing all the result set rows
```php
try {
$user = DB::select('SELECT * FROM my_table WHERE firstname = ?', ['John']);
} catch (PDOException $e) {
echo $e->getMessage();
}
```

The `single` statement is used to fetch the next row from a result set
```php
try {
$user = DB::single('SELECT * FROM my_table WHERE firstname = ?', ['John']);
} catch (PDOException $e) {
echo $e->getMessage();
}
```

The `insert` statement is used to insert new records in a table
```php
try {
$id = DB::insert('INSERT INTO my_table (firstname, lastname) VALUES (?,?)', ['John', 'Doe']);
} catch (PDOException $e) {
echo $e->getMessage();
}
```

The `insertAssoc` statement is used to insert new records in a table using an associative array
```php
$data = [
'firstname' => 'John',
'lastname' => 'Doe'
];

try {
$id = DB::insertAssoc('my_table', $data);
} catch (PDOException $e) {
echo $e->getMessage();
}
```

The `update` statement is used to modify the existing records in a table
```php
try {
DB::update('UPDATE my_table SET firstname = ? WHERE lastname = ?', ['John', 'Doe']);
} catch (PDOException $e) {
echo $e->getMessage();
}
```

The `delete` statement is used to delete existing records in a table
```php
try {
DB::delete('DELETE FROM my_table WHERE firstname = ?', ['John']);
} catch (PDOException $e) {
echo $e->getMessage();
}
```

Use `statement` for other operations not mentioned
```php
try {
DB::statement('DROP TABLE my_table');
} catch (PDOException $e) {
echo $e->getMessage();
}
```

### Transactions
Transaction are used to run a series of operations within an entity. If an exception is thrown between `beginTransaction` and `commit`, the transaction will automatically be rolled back.
```php
$data = [
'firstname' => 'John',
'lastname' => 'Doe'
];

try {
DB::beginTransaction();
$id = DB::insertAssoc('my_table', $data);
DB::update('UPDATE my_table SET firstname = ?, lastname = ? WHERE id = ?', ['Jane', 'Doe', $id]);
DB::commit();
} catch (PDOException $e) {
echo $e->getMessage();
}
```
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "riculum/php-pdo",
"description": "A complete database toolkit written in PHP to handle MySQL statements",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Riculum",
"email": "riculum@outlook.com"
}
],
"autoload": {
"psr-4": {
"Database\\": "./"
}
},
"require": {
"php": ">=7.4.0",
"vlucas/phpdotenv": "^5.3"
}
}
Loading

0 comments on commit 0d9c524

Please sign in to comment.