<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.mysql-xdevapi.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'mysql-xdevapi.examples.php',
    1 => 'Beispiele',
    2 => 'Beispiele',
  ),
  'up' => 
  array (
    0 => 'book.mysql-xdevapi.php',
    1 => 'Mysql_xdevapi',
  ),
  'prev' => 
  array (
    0 => 'changelog.mysql_xdevapi.php',
    1 => 'Changelog',
  ),
  'next' => 
  array (
    0 => 'ref.mysql-xdevapi.php',
    1 => 'Mysql_xdevapi-Funktionen',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'reference/mysql_xdevapi/examples.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mysql-xdevapi.examples" class="chapter">
 <h1 class="title">Beispiele</h1>

 <p class="para">
  Der zentrale Einstiegspunkt in die X DevAPI ist die Funktion
  <span class="function"><strong>mysql_xdevapi\getSession()</strong></span>, die einen URI zu einem MySQL
  8.0 Server benötigt und ein
  <span class="classname"><a href="class.mysql-xdevapi-session.php" class="classname">mysql_xdevapi\Session</a></span>-Objekt zurückgibt.
 </p>
 <div class="example" id="example-1">
  <p><strong>Beispiel #1 Verbindung zu einem MySQL-Server aufbauen</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try {<br />    </span><span style="color: #0000BB">$session </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_xdevapi\getSession</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysqlx://user:password@host"</span><span style="color: #007700">);<br />} catch(</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    die(</span><span style="color: #DD0000">"Die Verbindung konnte nicht hergestellt werden: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #FF8000">// ... $session verwenden<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

 </div>
 <p class="para">
  Die Session bietet einen vollständigen Zugriff auf die API. Bei einer
  neuen MySQL-Installation muss zunächst ein Datenbankschema mit einer
  Sammlung zum Speichern der Daten erstellt werden:
 </p>
 <div class="example" id="example-2">
  <p><strong>Beispiel #2 Erstellen eines Schemas und einer Sammlung auf dem MySQL-Server</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$schema </span><span style="color: #007700">= </span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createSchema</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$schema</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"example"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

 </div>
 <p class="para">
  Beim Speichern von Daten wird üblicherweise die Funktion
  <span class="function"><a href="function.json-encode.php" class="function">json_encode()</a></span> verwendet, um die Daten in JSON zu
  kodieren, die dann in einer Sammlung gespeichert werden können.
 </p>
 <p class="para">
  Im folgenden Beispiel werden Daten in der zuvor erstellten Sammlung
  gespeichert und Teile davon wieder abgerufen.
 </p>
 <div class="example" id="example-3">
  <p><strong>Beispiel #3 Speichern und Abrufen von Daten</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$marco </span><span style="color: #007700">= [<br />  </span><span style="color: #DD0000">"name" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Marco"</span><span style="color: #007700">,<br />  </span><span style="color: #DD0000">"age"  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">19</span><span style="color: #007700">,<br />  </span><span style="color: #DD0000">"job"  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Programmer"<br /></span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$mike </span><span style="color: #007700">= [<br />  </span><span style="color: #DD0000">"name" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Mike"</span><span style="color: #007700">,<br />  </span><span style="color: #DD0000">"age"  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">39</span><span style="color: #007700">,<br />  </span><span style="color: #DD0000">"job"  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Manager"<br /></span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$schema </span><span style="color: #007700">= </span><span style="color: #0000BB">$session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getSchema</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$schema</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"example"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$marco</span><span style="color: #007700">, </span><span style="color: #0000BB">$mike</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">(</span><span style="color: #DD0000">"name = 'Mike'"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">fetchOne</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

  <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt
eine ähnliche Ausgabe wie:</p></div>
  <div class="example-contents screen">
   <div class="cdata"><pre>
array(4) {
  [&quot;_id&quot;]=&gt;
  string(28) &quot;00005ad66aaf0000000000000003&quot;
  [&quot;age&quot;]=&gt;
  int(39)
  [&quot;job&quot;]=&gt;
  string(7) &quot;Manager&quot;
  [&quot;name&quot;]=&gt;
  string(4) &quot;Mike&quot;
}
</pre></div>
  </div>
 </div>
 <p class="para">
  Das folgende Beispiel zeigt, dass der MySQL-Server ein zusätzliches Feld
  namens <code class="code">_id</code> hinzufügt, das als Primärschlüssel für das Dokument
  dient.
 </p>
 <p class="para">
  Das Beispiel zeigt auch, dass die abgerufenen Daten alphabetisch sortiert
  sind. Diese spezifische Reihenfolge ergibt sich aus der effizienten
  Binärspeicherung im MySQL-Server, aber man sollte sich nicht darauf
  verlassen. Einzelheiten dazu sind in der MySQL-Dokumentation zum Datentyp
  JSON zu finden.
 </p>
 <p class="para">
  Optional können die PHP-Iteratoren verwendet werden, um mehrere Dokumente
  abzurufen:
 </p>
 <div class="example" id="example-4">
  <p><strong>Beispiel #4 Abrufen und Durchlaufen mehrerer Dokumente</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />foreach (</span><span style="color: #0000BB">$result </span><span style="color: #007700">as </span><span style="color: #0000BB">$doc</span><span style="color: #007700">) {<br />  echo </span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$doc</span><span style="color: #007700">[</span><span style="color: #DD0000">"name"</span><span style="color: #007700">]}</span><span style="color: #DD0000"> is a </span><span style="color: #007700">{</span><span style="color: #0000BB">$doc</span><span style="color: #007700">[</span><span style="color: #DD0000">"job"</span><span style="color: #007700">]}</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

  <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt
eine ähnliche Ausgabe wie:</p></div>
  <div class="example-contents screen">
   <div class="cdata"><pre>
Marco is a Programmer.
Mike is a Manager.
</pre></div>
  </div>
 </div>
</div>
<?php manual_footer($setup); ?>