|
1 | 1 | To utilize this class, first import Mysqldbi.php into your project, and require it.
|
2 | 2 |
|
3 |
| -<pre> |
4 |
| -<code> |
| 3 | +```php |
5 | 4 | require_once('Mysqlidb.php');
|
6 |
| -</code> |
7 |
| -</pre> |
| 5 | +``` |
8 | 6 |
|
9 | 7 | After that, create a new instance of the class.
|
10 | 8 |
|
11 |
| -<pre> |
12 |
| -<code> |
| 9 | +```php |
13 | 10 | $db = new Mysqlidb('host', 'username', 'password', 'databaseName');
|
14 |
| -</code> |
15 |
| -</pre> |
| 11 | +``` |
16 | 12 |
|
17 | 13 | Next, prepare your data, and call the necessary methods.
|
18 | 14 |
|
19 |
| -<h3> Insert Query </h3> |
20 |
| -<pre> |
21 |
| -<code> |
| 15 | +### Insert Query |
22 | 16 |
|
| 17 | +```php |
23 | 18 | $insertData = array(
|
24 | 19 | 'title' => 'Inserted title',
|
25 | 20 | 'body' => 'Inserted body'
|
26 | 21 | );
|
27 | 22 |
|
28 |
| -if ( $db->insert('posts', $insertData) ) echo 'success!'; |
| 23 | +if($db->insert('posts', $insertData)) echo 'success!'; |
| 24 | +``` |
29 | 25 |
|
30 |
| -</code> |
31 |
| -</pre> |
| 26 | +### Select Query |
32 | 27 |
|
33 |
| -<h3> Select Query </h3> |
34 |
| - |
35 |
| -<pre> |
36 |
| -<code> |
| 28 | +```php |
37 | 29 | $results = $db->get('tableName', 'numberOfRows-optional');
|
38 | 30 | print_r($results); // contains array of returned rows
|
39 |
| -</code> |
40 |
| -</pre> |
| 31 | +``` |
41 | 32 |
|
42 |
| -<h3> Update Query </h3> |
| 33 | +### Update Query |
43 | 34 |
|
44 |
| -<pre> |
45 |
| -<code> |
| 35 | +```php |
46 | 36 | $updateData = array(
|
47 | 37 | 'fieldOne' => 'fieldValue',
|
48 | 38 | 'fieldTwo' => 'fieldValue'
|
49 | 39 | );
|
50 | 40 | $db->where('id', int);
|
51 | 41 | $results = $db->update('tableName', $updateData);
|
52 |
| -</code> |
53 |
| -</pre> |
| 42 | +``` |
54 | 43 |
|
55 |
| -<h3> Delete Query </h3> |
| 44 | +### Delete Query |
56 | 45 |
|
57 |
| -<pre> |
58 |
| -<code> |
| 46 | +```php |
59 | 47 | $db->where('id', int);
|
60 |
| -if ( $db->delete('posts') ) echo 'successfully deleted'; |
61 |
| -</code> |
62 |
| -</pre> |
| 48 | +if($db->delete('posts')) echo 'successfully deleted'; |
| 49 | +``` |
63 | 50 |
|
64 |
| -<h3> Generic Query Method </h3> |
| 51 | +### Generic Query Method |
65 | 52 |
|
66 |
| -<pre> |
67 |
| -<code> |
| 53 | +```php |
68 | 54 | $results = $db->query('SELECT * from posts');
|
69 | 55 | print_r($results); // contains array of returned rows
|
70 |
| -</code> |
71 |
| -</pre> |
| 56 | +``` |
72 | 57 |
|
73 |
| -<h3> Raw Query Method </h3> |
| 58 | +### Raw Query Method |
74 | 59 |
|
75 |
| -<pre> |
76 |
| -<code> |
| 60 | +```php |
77 | 61 | $params = array(3, 'My Title');
|
78 | 62 | $resutls = $db->rawQuery("SELECT id, title, body FROM posts WHERE id = ? AND tile = ?", $params);
|
79 | 63 | print_r($results); // contains array of returned rows
|
80 | 64 |
|
81 | 65 | // will handle any SQL query
|
82 | 66 |
|
83 | 67 | $params = array(10, 1, 10, 11, 2, 10);
|
84 |
| -$resutls = $db->rawQuery(" |
85 |
| -(SELECT a FROM t1 WHERE a = ? AND B = ? ORDER BY a LIMIT ?) |
86 |
| -UNION |
87 |
| -(SELECT a FROM t2 WHERE a = ? AND B = ? ORDER BY a LIMIT ?) |
88 |
| -", $params); |
| 68 | +$resutls = $db->rawQuery("(SELECT a FROM t1 WHERE a = ? AND B = ? ORDER BY a LIMIT ?) UNION(SELECT a FROM t2 WHERE a = ? AND B = ? ORDER BY a LIMIT ?)", $params); |
89 | 69 | print_r($results); // contains array of returned rows
|
90 |
| -</code> |
91 |
| -</pre> |
| 70 | +``` |
92 | 71 |
|
93 | 72 |
|
| 73 | +### Where Method |
| 74 | +This method allows you to specify the parameters of the query. |
94 | 75 |
|
95 |
| -<h3> Where Method </h3> |
96 |
| -<p>This method allows you to specify the parameters of the query.</p> |
97 |
| -<pre> |
98 |
| -<code> |
| 76 | +```php |
99 | 77 | $db->where('id', int);
|
100 | 78 | $db->where('title', string);
|
101 | 79 | $results = $db->get('tableName');
|
102 | 80 | print_r($results); // contains array of returned rows
|
| 81 | +``` |
| 82 | + |
| 83 | +Optionally you can use method chaining to call where multiple times without referencing your object over an over: |
103 | 84 |
|
104 |
| -Optionally you can use method chaining to call where multiple times without referancing your object over an over: |
| 85 | +```php |
105 | 86 | $results = $db
|
106 | 87 | ->where('id', 1)
|
107 | 88 | ->where('title', 'MyTitle')
|
108 | 89 | ->get('tableName');
|
109 |
| - |
110 |
| -</code> |
111 |
| -</pre> |
| 90 | +``` |
0 commit comments