-
Notifications
You must be signed in to change notification settings - Fork 788
/
Copy pathexamples.xml
145 lines (139 loc) · 3.52 KB
/
examples.xml
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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="mysql-xdevapi.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
&reftitle.examples;
<para>
The central entry point to the X DevAPI is the <function>mysql_xdevapi\getSession</function>
function, which receives a URI to a MySQL 8.0 Server and returns a
<classname>mysql_xdevapi\Session</classname> object.
</para>
<example>
<title>Connecting to a MySQL Server</title>
<programlisting role="php">
<![CDATA[
<?php
try {
$session = mysql_xdevapi\getSession("mysqlx://user:password@host");
} catch(Exception $e) {
die("Connection could not be established: " . $e->getMessage());
}
// ... use $session
?>
]]>
</programlisting>
</example>
<para>
The session provides full access to the API. For a new MySQL Server installation,
the first step is to create a database schema with a collection
to store data:
</para>
<example>
<title>Creating a Schema and Collection on the MySQL Server</title>
<programlisting role="php">
<![CDATA[
<?php
$schema = $session->createSchema("test");
$collection = $schema->createCollection("example");
?>
]]>
</programlisting>
</example>
<para>
When storing data, typically <function>json_encode</function> is used to encode
the data into JSON, which can then be stored inside a collection.
</para>
<para>
The following example stores data into the collection we created earlier,
and then retrieve parts of it again.
</para>
<example>
<title>Storing and Retrieving Data</title>
<programlisting role="php">
<![CDATA[
<?php
$marco = [
"name" => "Marco",
"age" => 19,
"job" => "Programmer"
];
$mike = [
"name" => "Mike",
"age" => 39,
"job" => "Manager"
];
$schema = $session->getSchema("test");
$collection = $schema->getCollection("example");
$collection->add($marco, $mike)->execute();
var_dump($collection->find("name = 'Mike'")->execute()->fetchOne());
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(4) {
["_id"]=>
string(28) "00005ad66aaf0000000000000003"
["age"]=>
int(39)
["job"]=>
string(7) "Manager"
["name"]=>
string(4) "Mike"
}
]]>
</screen>
</example>
<para>
The example demonstrates that the MySQL Server adds an extra field named
<code>_id</code>, which serves as primary key to the document.
</para>
<para>
The example also demonstrates that retrieved data is sorted alphabetically.
That specific order comes from the efficient binary storage inside the MySQL server, but
it should not be relied upon. Refer to the MySQL JSON datatype documentation for details.
</para>
<para>
Optionally use PHP's iterators to fetch multiple documents:
</para>
<example>
<title>Fetching and Iterating Multiple Documents</title>
<programlisting role="php">
<![CDATA[
<?php
$result = $collection->find()->execute();
foreach ($result as $doc) {
echo "{$doc["name"]} is a {$doc["job"]}.\n";
}
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Marco is a Programmer.
Mike is a Manager.
]]>
</screen>
</example>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->