@@ -66,116 +66,72 @@ $db = new Db('driver', 'host', 'username', 'password', 'databaseName', 'charset'
6666
6767### Select
6868
69- select title and content columns
69+ select all from the table * table1 *
7070``` php
71- $selectCustomFields = $ db->select(['article', ['title, content']], null, '3', '0', ['id' => 'ASC'] );
71+ $db->select('table1' );
7272```
73- select all from the table
73+ select * 1 * row from the table * table1 * where * id * == * 1 *
7474``` php
75- $selectAll = $ db->select('tableName' );
75+ $db->select('table1', ['id' => 1] );
7676```
77-
78- or select just one row
79-
77+ select columns * col1* and * col2* from the table * table1*
8078``` php
81- $select = $ db->select('tableName ', ['id' => 1 ]);
79+ $db->select(['table1 ', ['col1', 'col2'] ]);
8280```
83- example of use:
81+ select columns * col1* and * col2* from the table * table1* with limit start from * 0* to * 3* sorted by ascending
82+ ``` php
83+ $db->select(['table1', ['col1', 'col2']], '', '3', '0', ['id' => 'ASC']);
84+ ```
85+
86+ ##### Examples of use:
87+
8488``` php
89+ // select 1 article where id == 1
8590$article = $db->select('article', ['id' => 1]))
91+ // show
8692foreach ($article as $k => $v)
8793{
8894 echo '<p >' . $k . ': ' . $v . '</p >';
8995}
90- ```
91-
92- ### Insert
9396
94- Simple example
95- ``` php
96- $data = Array ("login" => "admin",
97- "firstName" => "John",
98- "lastName" => 'Doe'
99- );
100- $id = $db->insert ('users', $data);
101- if($id)
102- echo 'user was created. Id=' . $id;
97+ // select columns title and content from the table article
98+ $selectCustomCols = $db->select(['article', ['title', 'content']]);
99+ // show
100+ foreach ($selectCustomCols as $rows)
101+ {
102+ echo '<p >';
103+ foreach ($rows as $col_k => $col_v)
104+ {
105+ echo $col_k . ': ' . $col_v . '<br >';
106+ }
107+ echo '</p >';
108+ }
103109```
104110
105- Insert with functions use
106- ``` php
107- $data = Array (
108- 'login' => 'admin',
109- 'active' => true,
110- 'firstName' => 'John',
111- 'lastName' => 'Doe',
112- 'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")),
113- // password = SHA1('secretpassword+salt')
114- 'createdAt' => $db->now(),
115- // createdAt = NOW()
116- 'expires' => $db->now('+1Y')
117- // expires = NOW() + interval 1 year
118- // Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
119- );
120-
121- $id = $db->insert ('users', $data);
122- if ($id)
123- echo 'user was created. Id=' . $id;
124- else
125- echo 'insert failed: ' . $db->getLastError();
126- ```
111+ ### Insert
127112
128- Insert with on duplicate key update
113+ insert one row into the table * table1 * with columns * id * , * title * , * content *
129114``` php
130- $data = Array ("login" => "admin",
131- "firstName" => "John" ,
132- "lastName" => 'Doe ',
133- "createdAt" => $db->now(),
134- "updatedAt" => $db->now(),
115+ $db->insert('table1', [
116+ 'id' => null ,
117+ 'title' => 'Заголовок 1 ',
118+ 'content' => 'Тут текст записи № 1.'
119+ ]
135120);
136- $updateColumns = Array ("updatedAt");
137- $lastInsertId = "id";
138- $db->onDuplicate($updateColumns, $lastInsertId);
139- $id = $db->insert ('users', $data);
140121```
141122
142123### Insert Multiple
143124
144- Insert multiple datasets at once
125+ insert several rows (if id PRIMARY KEY - we can use null)
145126``` php
146- $data = Array(
147- Array ("login" => "admin",
148- "firstName" => "John",
149- "lastName" => 'Doe'
150- ),
151- Array ("login" => "other",
152- "firstName" => "Another",
153- "lastName" => 'User',
154- "password" => "very_cool_hash"
155- )
127+ $db->insertMultiple(
128+ 'table1',
129+ ['id, title, content'],
130+ [
131+ [1, 'title 1', 'Text of the article 1'],
132+ [2, 'title 2', 'Text of the article 2']
133+ ]
156134);
157- $ids = $db->insertMulti('users', $data);
158- if(!$ids) {
159- echo 'insert failed: ' . $db->getLastError();
160- } else {
161- echo 'new users inserted with following id\'s: ' . implode(', ', $ids);
162- }
163- ```
164-
165- If all datasets only have the same keys, it can be simplified
166- ``` php
167- $data = Array(
168- Array ("admin", "John", "Doe"),
169- Array ("other", "Another", "User")
170- );
171- $keys = Array("login", "firstName", "lastName");
172-
173- $ids = $db->insertMulti('users', $data, $keys);
174- if(!$ids) {
175- echo 'insert failed: ' . $db->getLastError();
176- } else {
177- echo 'new users inserted with following id\'s: ' . implode(', ', $ids);
178- }
179135```
180136
181137### Update
@@ -205,7 +161,7 @@ $db->update ('users', $data, 10);
205161### Delete
206162
207163``` php
208- $db->delete('id ', 1 );
164+ $db->delete('table1 ', ['id' => 1] );
209165```
210166example of use:
211167``` php
@@ -218,7 +174,7 @@ if($db->delete('article', ['id' => 1]))
218174### Create Database
219175
220176``` php
221- $db->createDatabase('articles ');
177+ $db->createDatabase('database1 ');
222178```
223179example of use:
224180``` php
@@ -256,7 +212,7 @@ if($db->createTable($sql))
256212### Truncate Table
257213
258214``` php
259- $db->truncateTable('article ');
215+ $db->truncateTable('table1 ');
260216```
261217example of use:
262218``` php
@@ -269,7 +225,7 @@ if($db->truncateTable('article'))
269225### Drop Database
270226
271227``` php
272- $db->dropDatabase('articles ');
228+ $db->dropDatabase('database1 ');
273229```
274230example of use:
275231``` php
@@ -282,7 +238,7 @@ if($db->dropDatabase('articles'))
282238### Drop Table
283239
284240``` php
285- $db->dropTable('article ');
241+ $db->dropTable('table1 ');
286242```
287243example of use:
288244``` php
0 commit comments