<?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 => 'es',
  ),
  'this' => 
  array (
    0 => 'mysql-xdevapi.examples.php',
    1 => 'Ejemplos',
    2 => 'Ejemplos',
  ),
  'up' => 
  array (
    0 => 'book.mysql-xdevapi.php',
    1 => 'Mysql_xdevapi',
  ),
  'prev' => 
  array (
    0 => 'changelog.mysql_xdevapi.php',
    1 => 'Registro de cambios',
  ),
  'next' => 
  array (
    0 => 'ref.mysql-xdevapi.php',
    1 => 'Funciones Mysql_xdevapi',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'es',
    '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">Ejemplos</h1>

 <p class="para">
  El punto de entrada central de la API X es la función <span class="function"><strong>mysql_xdevapi\getSession()</strong></span>,
  que recibe una URI hacia un servidor MySQL 8.0 y devuelve un objeto
  <span class="classname"><a href="class.mysql-xdevapi-session.php" class="classname">mysql_xdevapi\Session</a></span>.
 </p>
 <div class="example" id="example-1">
  <p><strong>Ejemplo #1 Conectar a un Servidor MySQL</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">"La conexión no pudo ser establecida: " </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">// ... usar $session<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

 </div>
 <p class="para">
  La sesión proporciona acceso completo a la API. Para una nueva instalación de servidor MySQL,
  el primer paso es crear un esquema de base de datos con una colección
  para almacenar datos:
 </p>
 <div class="example" id="example-2">
  <p><strong>Ejemplo #2 Crear un Esquema y una Colección en el Servidor MySQL</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">
  Al almacenar datos, generalmente <span class="function"><a href="function.json-encode.php" class="function">json_encode()</a></span> se utiliza para codificar
  los datos en JSON, que luego pueden ser almacenados en una colección.
 </p>
 <p class="para">
  Los siguientes ejemplos almacenan datos en la colección que hemos creado anteriormente,
  y luego recuperan partes de estos datos nuevamente.
 </p>
 <div class="example" id="example-3">
  <p><strong>Ejemplo #3 Almacenar y Recuperar Datos</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>Resultado del ejemplo anterior es similar a:</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">
  Este ejemplo demuestra que el servidor MySQL añade un campo adicional llamado
  <code class="code">_id</code>, que sirve como clave primaria del documento.
 </p>
 <p class="para">
  Este ejemplo también demuestra que los datos recuperados están ordenados alfabéticamente.
  Este orden específico proviene del almacenamiento binario eficiente dentro del servidor MySQL, pero
  no debe confiarse en él. Consulte la documentación del tipo de datos JSON de MySQL para más detalles.
 </p>
 <p class="para">
  Opcionalmente, utilice los iteradores de PHP para recuperar múltiples documentos:
 </p>
 <div class="example" id="example-4">
  <p><strong>Ejemplo #4 Recuperar e Iterar sobre Múltiples Documentos</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"> es un </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>Resultado del ejemplo anterior es similar a:</p></div>
  <div class="example-contents screen">
   <div class="cdata"><pre>
Marco es un Programmer.
Mike es un Manager.
</pre></div>
  </div>
 </div>
</div>
<?php manual_footer($setup); ?>