An open API service indexing awesome lists of open source software.

https://github.com/samuelfaj/php-class_db.php

A PHP's Facilitation Class for Single and Multiple Databases.
https://github.com/samuelfaj/php-class_db.php

connection database mssql mysql mysqli pgsql php sqlserv

Last synced: 5 months ago
JSON representation

A PHP's Facilitation Class for Single and Multiple Databases.

Awesome Lists containing this project

README

          

# Class_DB

![MIT license](https://img.shields.io/badge/license-MIT-blue.svg)
[![built with PHP](https://img.shields.io/badge/built%20with-PHP-red.svg)](https://www.php.net/)
![MySQL Ready](https://img.shields.io/badge/mysql-ready-green.svg)
![MySQLI Ready](https://img.shields.io/badge/mysqli-ready-green.svg)
![mssql Ready](https://img.shields.io/badge/mssql-ready-green.svg)
![sqlserv Ready](https://img.shields.io/badge/sqlserv-ready-green.svg)
![pgsql Ready](https://img.shields.io/badge/pgsql-ready-green.svg)

*Write less. Do a lot more.*
**Class_DB allows you to write secure SQL queris easily and work with many different databases using the same syntax**

Everytime i was working with or starting a new system, i had to look at the database documentation and choose between create a class for it using mine functions or write the system following his own. But what if the driver changed? Yeah, Everyone who is here for a while saw some changes in PHP drivers. Those changes were a painful work for great systems and even more for small teams. We saw mssql become sqlserv, mysql become mysqli and it's common to see groups deciding to change the database in the middle of the project.

Thinking about it i developed class_db. It helps us to write less SQL and don't care for the chosen database.
Just write your code and class_db will do the rest. If the driver or the team resolves to change, just update it!

### Install

It's simple, just download and require it.

```sh
composer require samuelfaj/class_db
```

## Examples

Initializing
```php
'localhost' , // string - Host of Connection.
'user' => 'username' , // string - Database's User.
'password' => 'mysecretpass' , // string - User's Password.
'database' => 'myapplication' , // string - Default Database name.
'db_type' => 'mysql' , // string - Type of Database. (It can be: 'mysql', 'mysqli' , 'mssql' , 'sqlserv' , 'pgsql').
));

$sql = new ClassDb\Query($db);
```

Doing a simple query passing it as text:
```php
'localhost' , // string - Host of Connection.
'user' => 'username' , // string - Database's User.
'password' => 'mysecretpass' , // string - User's Password.
'database' => 'myapplication' , // string - Default Database name.
'db_type' => 'mysql' , // string - Type of Database. (It can be: 'mysql', 'mysqli' , 'mssql' , 'sqlserv' , 'pgsql').
));

$sql = new ClassDb\Query($db);
$sql->exec("SELECT * FROM users");
var_dump($sql->query);
```

Doing a select without write one single character of SQL:
```php
'localhost' , // string - Host of Connection.
'user' => 'username' , // string - Database's User.
'password' => 'mysecretpass' , // string - User's Password.
'database' => 'myapplication' , // string - Default Database name.
'db_type' => 'mysql' , // string - Type of Database. (It can be: 'mysql', 'mysqli' , 'mssql' , 'sqlserv' , 'pgsql').
));

$sql->table('users');
$sql->limit(array(0,10));

var_dump($sql->select());
```

Selecting and updating a user without write one single character of SQL:
```php
table('users');
$sql->where(
array('id' , $_POST['id']),
array('email' , $_POST['email'])
);
$sql->order('id','DESC');
$sql->limit(1);

if( $sql->select()->have_rows ){
$sql->update(array('active' => 0));
}
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details