forked from ntemple/mysql-keyvalue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv_example.php
186 lines (151 loc) · 3.46 KB
/
kv_example.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!doctype html>
<html>
<head>
<title>PHP PDO key/value abstraction layer</title>
<style>
html{
font:12px/24px verdana;
}
h1{
font:16px/32px arial;
font-weight:bold;
margin-top: 40px;
}
code{
border: 1px solid black;
padding: 10px;
margin: 10px;
white-space:pre;
display:block;
}
</style>
</head>
<body>
<h1>PHP PDO key/value abstraction layer</h1>
<a href="kv.phps">source</a>, <a href="/kv-phpdoc">phpdoc</a>
<?php
require_once('kv.php');
?>
<h1>create test database</h1>
<code>
require_once('kv.php');
$ds = new kvstore(new PDO($dsn,$user,$pass),'table-0');
$ds->dropTable();
$ds->createTable();
$ds->errorExceptions();
//$ds->errorSilent();
//$ds->errorWarning();
</code>
<?php
$ds = new kvstore(new PDO('mysql:host=db62.1and1.pl;dbname=db339205542','dbo339205542','qazwsxedcrfv'));
$ds->dropTable();
$ds->createTable();
$ds->errorExceptions();
?>
<h1>insert sample value and retrive it</h1>
<code>
$ds->set('key', 'value');
$v = $ds->get('key');
</code>
<?php
$ds->set('key', 'value');
$v = $ds->get('key');
var_dump($v);
?>
<h1>insert array of values</h1>
<code>
$mkv = array(
'usr:0001' => 'First user',
'usr:0002' => 'Second user',
'usr:0003' => 'Third user'
);
$ds->mset($mkv);
</code>
<?php
$mkv = array(
'usr:0001' => 'First user',
'usr:0002' => 'Second user',
'usr:0003' => 'Third user'
);
$ds->mset($mkv);
?>
<h1>Retrive as array [ [k,v],[k,v] ] </h1>
<code>
$x = $ds->mget_assoc(array_keys($mkv));
</code>
<?php
$x = $ds->mget_assoc(array_keys($mkv));
?><pre><?php print_r($x); ?></pre>
<h1>as array [[k=>v],[k=>v]]</h1>
<code>
$x = $ds->mget(array_keys($mkv));
</code>
<?php
$x = $ds->mget(array_keys($mkv));
?>
<pre><?php print_r($x); ?></pre>
<h1>increment test:</h1>
<code>
$x = $ds->incr('counter1');
$x = $ds->incr('counter2');
$x = $ds->incr('counter2');
$x = $ds->incr('counter2');
</code>
<?php
$x = $ds->incr('counter1');
$x = $ds->incr('counter2');
$x = $ds->incr('counter2');
$x = $ds->incr('counter2');
var_dump($x);
?>
<h1>get array of keys via mget after incrementation</h1>
<code>
$x = $ds->mget(array('counter1', 'counter2'));
</code>
<?php
$x = $ds->mget(array('counter1', 'counter2'));
?><pre><?php print_r($x); ?></pre>
<h1>push values</h1>
<code>
$ds->rpush('list', 'a');
$ds->rpush('list', 'b');
$ds->lpush('list', 'c');
$ds->rpush('list', array('e'=>'f'));
print_r($ds->get('list'));
</code>
<?php
$ds->rpush('list', 'a');
print_r($ds->get('list'));echo '<br />';
$ds->rpush('list', 'b');
print_r($ds->get('list'));echo '<br />';
$ds->lpush('list', 'c');
print_r($ds->get('list'));echo '<br />';
$ds->rpush('list', array('e'=>'f'));
print_r($ds->get('list'));echo '<br />';
?>
<h1>get results like key</h1>
<code>
$x = $ds->getLike('usr%');
</code>
<?php
$x = $ds->getLike('usr%');
?><pre><?php print_r($x); ?></pre>
<h1>get object as JSON</h1>
<code>
$x = $ds->getJSON('list');
</code>
<?php
$x = $ds->getJSON('list');
?><pre><?php print_r($x); ?></pre>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-4497317-8']);
_gaq.push(['_setDomainName', 'rashell.pl']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body></html>