|
1 | 1 | # db-class |
2 | | -A simple database class with php and pdo. |
3 | 2 |
|
4 | | -More information coming soon! |
| 3 | +This project is a simple database class with php and pdo. |
| 4 | + |
| 5 | +## Installation/Setup |
| 6 | + |
| 7 | +If you want to use the database class for your own project, you can simply follow these instructions: |
| 8 | + |
| 9 | +1. Download the ZIP file of this project |
| 10 | +2. Unzip it and save the file `DB.php` to your own project directory. |
| 11 | +3. If all requirements, like PHP, PDO and a database that fits to that setup, are fulfilled, you should now be ready to start! |
| 12 | + |
| 13 | +## Basic Usage |
| 14 | + |
| 15 | +If you have successfully "installed" everything, you can use the class like that: |
| 16 | + |
| 17 | +### Require database class |
| 18 | +```php |
| 19 | +require_once('your_path/DB.php'); |
| 20 | +``` |
| 21 | + |
| 22 | +### Instantiate Class / Connect to Database |
| 23 | + |
| 24 | +To be able to use the class and connect to the database, you have to instantiate it. |
| 25 | + |
| 26 | +To do that, follow that format: |
| 27 | + |
| 28 | +```php |
| 29 | +new DB(string $dbname, string $user, string $password, string $db_type='mysql', string $host='localhost', int $pdo_err_mode=PDO::ERRMODE_EXCEPTION) |
| 30 | +``` |
| 31 | + |
| 32 | +It's providing these options: |
| 33 | + |
| 34 | +#### Database Name ('$dbname') |
| 35 | +The name of your database you want to connect with. (required) |
| 36 | + |
| 37 | +#### Database User ('$user') |
| 38 | +The user of the database that should interact with it. (required) |
| 39 | + |
| 40 | +#### Password of the Database User ('$password') |
| 41 | +The password of the database user you are using to interact with the database. (required) |
| 42 | + |
| 43 | +#### Database Type ('$db_type') |
| 44 | +The type of the database you are using. (optional) |
| 45 | + |
| 46 | +Default: `mysql` |
| 47 | + |
| 48 | +#### Host ('$host') |
| 49 | +The host of your database. (optional) |
| 50 | + |
| 51 | +Default: `localhost` |
| 52 | + |
| 53 | +#### PDO Error Mode ('$pdo_err_mode') |
| 54 | +The [error mode of pdo](http://php.net/manual/en/pdo.error-handling.php) you want to use. (optional) |
| 55 | + |
| 56 | +Default: `PDO::ERRMODE_EXCEPTION` |
| 57 | + |
| 58 | +Simple example for instantiating the class: |
| 59 | +```php |
| 60 | +$db = new DB('db-class-example', 'root', ''); |
| 61 | +``` |
| 62 | + |
| 63 | +### Configure Error Handling |
| 64 | + |
| 65 | +If you want to, you can create your own error handling setup before you instantiate the class. |
| 66 | + |
| 67 | +Important to know is that every method returns the array `$error` on failure with some basic information about the error that is occured. |
| 68 | + |
| 69 | +The following options exist: |
| 70 | + |
| 71 | +#### Environment (`$env`) |
| 72 | +`production` or `development`/`dev` |
| 73 | + |
| 74 | +Production: return simple error code and the related error message (default) |
| 75 | + |
| 76 | +Development: return simple error code, the related error message and the [`PDOException Object`](http://php.net/manual/en/class.pdoexception.php) |
| 77 | + |
| 78 | +#### Error Types / Error Messages ('$error_types') |
| 79 | +An array of the error messages with the error code as the key. |
| 80 | + |
| 81 | +Default: |
| 82 | +```php |
| 83 | +[ |
| 84 | + 0 => 'success', |
| 85 | + 1 => 'Connection to database failed', |
| 86 | + 2 => 'Selecting/Getting data from database failed', |
| 87 | + 3 => 'Inserting data into database failed', |
| 88 | + 4 => 'Deleting data from database failed', |
| 89 | + 5 => 'Updating data in database failed', |
| 90 | +] |
| 91 | +``` |
| 92 | + |
| 93 | +**Attention**: Do not change the error codes/keys as long as you don't modify the class according to that! When you change the error code of an error and then just use the database class as normal, it will not work as expected! |
| 94 | + |
| 95 | +Besides from that you can freely change the error messages to your own liking. |
| 96 | + |
| 97 | +To change the config of the error handling, you must call the static method `initErrorHandler(array $error_types=[], string $env='production')`, which will basically set your specified configs, **before** you are instantiating the class. |
| 98 | + |
| 99 | +Example: |
| 100 | +```php |
| 101 | +DB::initErrorHandler( |
| 102 | + [ |
| 103 | + 0 => 'success', |
| 104 | + 1 => 'Sorry, the connection to the database is failed!', |
| 105 | + 2 => 'Sorry, we are currently not able to receive data from the database!', |
| 106 | + 3 => 'Sorry, we are currently not able to insert your data to the database!', |
| 107 | + 4 => 'Sorry, we are currently not able to delete your data from the database!', |
| 108 | + 5 => 'Sorry, we are currently not able to update your data in the database!', |
| 109 | + ] |
| 110 | +); |
| 111 | +``` |
| 112 | + |
| 113 | +Always make sure to pass in the whole array, e.g. not just error 2 and 5, because then only error 2 and 5 will exist. |
| 114 | + |
| 115 | +In case you don't want to change the messages, but you would like to switch the environment, you have to pass in an empty array as the first argument like that: |
| 116 | + |
| 117 | +```php |
| 118 | +DB::initErrorHandler( |
| 119 | + [], |
| 120 | + 'development' |
| 121 | +); |
| 122 | +``` |
0 commit comments