1
- Simple static class for working with MySql databases
1
+ Simple static/non-static classes for working with MySql databases
2
2
* ** PHP version:** 7.0+
3
3
* ** Composer:** ` composer require power/db `
4
4
* This code was taken as a basis https://github.com/colshrapnel/safemysql
5
+
6
+ #### Two ways to use: static and non/static
7
+ All methods available in both class types
8
+ ``` php
9
+ use \Power\DB;
10
+
11
+ // Non static
12
+ $db = new \Power\mDB('localhost', 'users', 'passwd', 'dbname', 'utf8mb4');
13
+ // Select all records from users table
14
+ $data = $db->getAll('SELECT * FROM ?n', 'users');
15
+
16
+ // Static
17
+ DB::Init('localhost', 'users', 'passwd', 'dbname', 'utf8mb4');
18
+ // Select all records from users table
19
+ $data = DB::getAll('SELECT * FROM ?n', 'users');
20
+ ```
21
+
5
22
#### Work with one database
6
23
``` php
7
24
use \Power\DB;
@@ -22,16 +39,21 @@ DB::getOne('SELECT count(*) FROM `users`');
22
39
// Insert some data
23
40
$table_name = 'logs';
24
41
$data = [
25
- 'create_date' => [ 'now()'] , // when the value is an array it will not be escaped, useful for functions or someone else code
42
+ 'create_date' => DB::pure( 'now()') , // when you don't need to escape value - use DB::pure method
26
43
'login' => 'tester',
27
44
'userid' => 5
28
45
];
29
46
DB::query('INSERT INTO ?n SET ?u', $table_name, $data);
47
+ // or user insert method
48
+ DB::insert($table_name, $data);
30
49
// Get inserted id from last query
31
50
echo DB::insertId();
51
+
52
+ // Update records
53
+ DB::update($table_name, $data)
32
54
```
33
55
34
- #### Work with several databases
56
+ #### Work with several databases from static class
35
57
``` php
36
58
use \Power\DB;
37
59
0 commit comments