php-doc-en/reference/mysql_xdevapi/examples.xml
Philip Olson 98bf51d2b9 Documented the mysql_xdevapi PECL extension, an extension for the MySQL X DevAPI.
Documentation written by Darek, Filip, Johannes, and myself
They are rough and incomplete at the moment so translators
please ignore them for at least a month


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@344726 c90b9560-bf6c-de11-be94-00142212c4b1
2018-04-19 05:05:47 +00:00

144 lines
3.6 KiB
XML

<?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_xdevap\Session</classname> object.
</para>
<example>
<title>Connecting to a MySQL Server</title>
<programlisting role="php">
<![CDATA[
<?php
$session = mysql_xdevapi\getSession("mysqlx://user:password@host");
if ($session === NULL) {
die("Connection could not be established");
}
// ... 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 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
-->