php-doc-en/reference/mysql_xdevapi/examples.xml
George Peter Banyard dd11e151f1 Fix Doc Bug #77852
Correct typo in Example #4 - removes extra ')' at end of '$result = ...' line.

-- 
Provided by anonymous 97354 (damien.harwin@mayden.co.uk)

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@347152 c90b9560-bf6c-de11-be94-00142212c4b1
2019-04-05 19:58:43 +00:00

145 lines
3.5 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
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 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
-->