https://github.com/sujanshresthanet/pdo-wrapper
Pdo Wrapper
https://github.com/sujanshresthanet/pdo-wrapper
mysql pdo php
Last synced: about 2 months ago
JSON representation
Pdo Wrapper
- Host: GitHub
- URL: https://github.com/sujanshresthanet/pdo-wrapper
- Owner: sujanshresthanet
- License: gpl-3.0
- Created: 2022-06-07T17:04:22.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-07T17:12:21.000Z (almost 3 years ago)
- Last Synced: 2025-01-14T10:49:01.722Z (3 months ago)
- Topics: mysql, pdo, php
- Language: PHP
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [](https://packagist.org/packages/sujanshresthanet/pdo-wrapper) [](https://packagist.org/packages/sujanshresthanet/pdo-wrapper)
PDO Database Class
============================#### 1. Add via composer :
```
composer require sujanshresthanet/pdo-wrapper
```
#### 2. Extend your class db and your object model :```php
#Create class MyDb for example and extends from \sujanshresthanet\pdo\Db.
#To use the class
#if you want object model from table mysql you can use like these following class Persons with extends from Crudrequire(__DIR__ . './config/database.php');
class MyDb extends \sujanshresthanet\pdo\Db {
public function __construct() {
parent::__construct(HOST, DB_NAME, DB_USER, DB_PASS);
}
}//create class Persons if we want
class Persons extends sujanshresthanet\pdo\Crud {protected $table = 'persons';
# Primary Key of the table
protected $pk = 'id';public $className = 'MyDb';
}
```#### 3. Bootstraping in your project example in index.php
```php
query("SELECT * FROM persons");
echo "";print_r($persons);$person = new Persons();
// Create new person
$person->Firstname = "Josh";
$person->Age = "20";
$person->Sex = "F";
$created = $person->create();// Or give the bindings to the constructor
$person = new Persons(array("Firstname" => "Josh", "age" => "20", "sex" => "F"));
$created = $person->create();```
#### 4. Logs - Modify the read/write rights of the root folder
Everytime an exception is thrown by the database class a log file gets created or modified.
These logs are stored in the logs directory. Which means the database class needs write access for the logs folder.
If the files are on a webserver you'll have to modify the rights of the root folder otherwise you'll get a "Permission denied" error.The log file is a simple plain text file with the current date('year-month-day') as filename.
## Examples
Below some examples of the basic functions of the database class. I've included a SQL dump so you can easily test the database
class functions.
#### The persons table
| id | firstname | lastname | sex | age
|:-----------:|:------------:|:------------:|:------------:|:------------:|
| 1 | John | Doe | M | 19
| 2 | Bob | Black | M | 41
| 3 | Zoe | Chan | F | 20
| 4 | Josh | Martin | M | 14
| 5 | Adam| Rose | M | 56#### Fetching everything from the table
```php
query("SELECT * FROM persons");
```
#### Fetching with Bindings (ANTI-SQL-INJECTION):
Binding parameters is the best way to prevent SQL injection. The class prepares your SQL query and binds the parameters
afterwards.There are three different ways to bind parameters.
```php
bind("id","1");
$db->bind("firstname","John");
$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id");// 2. Bind more parameters
$db->bindMore(array("firstname"=>"John","id"=>"1"));
$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id"));// 3. Or just give the parameters to the method
$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname",array("firstname"=>"John","id"=>"1"));
```More about SQL injection prevention : http://indieteq.com/index/readmore/how-to-prevent-sql-injection-in-php
#### Fetching Row:
This method always returns only 1 row.
```php
row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1"));
```
##### Result
| id | firstname | lastname | sex | age
|:-----------:|:------------:|:------------:|:------------:|:------------:|
| 1 | John | Doe | M | 19
#### Fetching Single Value:
This method returns only one single value of a record.
```php
bind("id","3");
$firstname = $db->single("SELECT firstname FROM Persons WHERE id = :id");
```
##### Result
|firstname
|:------------:
| Zoe
#### Fetching Column:
```php
column("SELECT Firstname FROM Persons");
```
##### Result
|firstname |
|:-----------:
| John
| Bob
| Zoe
| Josh
| Adam
### Delete / Update / Insert
When executing the delete, update, or insert statement by using the query method the affected rows will be returned.
```php
query("DELETE FROM Persons WHERE Id = :id", array("id"=>"1"));// Update
$update = $db->query("UPDATE Persons SET firstname = :f WHERE Id = :id", array("f"=>"Jan","id"=>"32"));// Insert
$insert = $db->query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)", array("f"=>"Tyler","age"=>"20"));// Do something with the data
if($insert > 0 ) {
return 'Succesfully created a new person !';
}```
## Method parameters
Every method which executes a query has the optional parameter called bindings.The row and the query method have a third optional parameter which is the fetch style.
The default fetch style is PDO::FETCH_ASSOC which returns an associative array.Here an example :
```php
row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1"), PDO::FETCH_NUM);print_r($person_num);
// Array ( [0] => 1 [1] => Johny [2] => Doe [3] => M [4] => 19 )```
More info about the PDO fetchstyle : http://php.net/manual/en/pdostatement.fetch.phpEasyCRUD
============================
The easyCRUD is a class which you can use to easily execute basic SQL operations like(insert, update, select, delete) on your database.
It uses the database class I've created to execute the SQL queries.Actually it's just a little ORM class.
## How to use easyCRUD
#### 1. First, create a new class. Then require the easyCRUD class.
#### 2. Extend your class to the base class Crud and add the following fields to the class.
#### Example class :
```php
Firstname = "Josh";
$person->Age = "20";
$person->Sex = "F";
$created = $person->Create();// Or give the bindings to the constructor
$person = new person(array("Firstname"=>"Josh","age"=>"20","sex"=>"F"));
$created = $person->Create();// SQL Equivalent
"INSERT INTO persons (Firstname,Age,Sex) VALUES ('Josh','20','F')"
```
#### Deleting a person
```php
Id = "17";
$deleted = $person->Delete();// Shorthand method, give id as parameter
$deleted = $person->Delete(17);// SQL Equivalent
"DELETE FROM persons WHERE Id = 17 LIMIT 1"
```
#### Saving person's data
```php
Firstname = "John";
$person->Age = "20";
$person->Sex = "F";
$person->Id = "4";
// Returns affected rows
$saved = $person->Save();// Or give the bindings to the constructor
$person = new person(array("Firstname"=>"John","age"=>"20","sex"=>"F","Id"=>"4"));
$saved = $person->Save();// SQL Equivalent
"UPDATE persons SET Firstname = 'John',Age = 20, Sex = 'F' WHERE Id= 4"
```
#### Finding a person
```php
Id = "1";
$person->Find();echo $person->firstname;
// Johny// Shorthand method, give id as parameter
$person->Find(1);// SQL Equivalent
"SELECT * FROM persons WHERE Id = 1"
```
#### Getting all the persons
```php
all();// SQL Equivalent
"SELECT * FROM persons
```