Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 10582

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Examples</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysql-xdevapi.constants.html">Predefined Constants</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.mysql-xdevapi.html">Mysql_xdevapi Functions</a></div>
 <div class="up"><a href="book.mysql-xdevapi.html">Mysql_xdevapi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysql-xdevapi.examples" class="chapter">
 <h1>Examples</h1>

 <p class="para">
  The central entry point to the X DevAPI is the <span class="function"><strong>mysql_xdevapi\getSession()</strong></span>
  function, which receives a URI to a MySQL 8.0 Server and returns a
  <strong class="classname">mysql_xdevap\Session</strong> object.
 </p>
 <div class="example" id="example-1970">
  <p><strong>Example #1 Connecting to a MySQL Server</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&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$session&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_xdevapi</span><span style="color: #007700">\</span><span style="color: #0000BB">getSession</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysqlx://user:password@host"</span><span style="color: #007700">);<br />}&nbsp;catch(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Connection&nbsp;could&nbsp;not&nbsp;be&nbsp;established:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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 />&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;...&nbsp;use&nbsp;$session<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <p class="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:
 </p>
 <div class="example" id="example-1971">
  <p><strong>Example #2 Creating a Schema and Collection on the MySQL Server</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$schema&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;</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">
  When storing data, typically <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> is used to encode
  the data into JSON, which can then be stored inside a collection.
 </p>
 <p class="para">
  The following example stores data into the collection we created earlier, 
  and then retrieve parts of it again.
 </p>
 <div class="example" id="example-1972">
  <p><strong>Example #3 Storing and Retrieving Data</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$marco&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Marco"</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"age"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">19</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"job"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Programmer"<br /></span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$mike&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Mike"</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"age"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">39</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">"job"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Manager"<br /></span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$schema&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</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&nbsp;=&nbsp;'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>The above example will output
something similar to:</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">
  The example demonstrates that the MySQL Server adds an extra field named
  <code class="code">_id</code>, which serves as primary key to the document.
 </p>
 <p class="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.
 </p>
 <p class="para">
  Optionally use PHP&#039;s iterators fetch multiple documents:
 </p>
 <div class="example" id="example-1973">
  <p><strong>Example #4 Fetching and Iterating Multiple Documents</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</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">&nbsp;is&nbsp;a&nbsp;</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>The above example will output
something similar to:</p></div>
  <div class="example-contents screen">
   <div class="cdata"><pre>
Marco is a Programmer.
Mike is a Manager.
</pre></div>
  </div>
 </div>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysql-xdevapi.constants.html">Predefined Constants</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.mysql-xdevapi.html">Mysql_xdevapi Functions</a></div>
 <div class="up"><a href="book.mysql-xdevapi.html">Mysql_xdevapi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>