Repository navigation
Expand file tree
/
Copy pathexample_users.php
More file actions
92 lines (78 loc) · 2.29 KB
/
Copy pathexample_users.php
File metadata and controls
92 lines (78 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
// example_users.php
//
// Example usage of VIBEDB with a `users` table:
//
// CREATE TABLE `users` (
// `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
// `CREATE_TIMESTAMP` DATETIME NOT NULL,
// `JSON` TEXT NULL,
// `first_name` VARCHAR(100) NOT NULL,
// `last_name` VARCHAR(100) NOT NULL,
// `email` VARCHAR(255) NOT NULL,
// `address` VARCHAR(255),
// `city` VARCHAR(100),
// `state` VARCHAR(50),
// `zip` VARCHAR(20),
// `phone` VARCHAR(50),
// PRIMARY KEY (`id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
require_once __DIR__ . '/class.VIBEDB.php';
$DB = new VIBEDB();
// 1. INSERT a new user
$insertResult = $DB->post('users', [
'first_name' => 'Alice',
'last_name' => 'Smith',
'email' => 'alice@example.com',
'address' => '123 Main St',
'city' => 'Chicago',
'state' => 'IL',
'zip' => '60601',
'phone' => '555-111-2222',
// Extra fields (will be stored in JSON column)
'nickname' => 'Ali',
'vip_level' => 'gold',
]);
$newUserId = $insertResult['id'] ?? null;
echo "Inserted user id: " . $newUserId . PHP_EOL;
// 2. UPDATE the user
$updateResult = $DB->post('users', [
'id' => $newUserId,
'phone' => '555-999-0000',
'vip_level' => 'platinum', // stays in JSON
]);
echo "Updated user id: " . $updateResult['id'] . PHP_EOL;
// 3. QUERY users (including virtual JSON columns)
$rows = $DB->sql("
SELECT
id,
first_name,
last_name,
nickname, -- virtual column from JSON
vip_level -- virtual column from JSON
FROM users
ORDER BY id DESC
LIMIT 10
");
echo "\nLast 10 users:\n";
foreach ($rows as $row) {
echo sprintf(
"[%d] %s %s (nickname: %s, vip: %s)\n",
$row['id'],
$row['first_name'],
$row['last_name'],
$row['nickname'] ?? '',
$row['vip_level'] ?? ''
);
}
// 4. Get a single user
$user = $DB->sqlOne("
SELECT id, first_name, last_name, nickname
FROM users
WHERE id = ?
", [$newUserId]);
echo "\nSingle user:\n";
print_r($user);
// 5. DELETE the user
$deleteResult = $DB->delete('users', $newUserId);
echo "\nDeleted user id: " . $deleteResult['id'] . PHP_EOL;