Beispiele

The central entry point to the X DevAPI is the mysql_xdevapi\getSession() function, which receives a URI to a MySQL 8.0 Server and returns a mysql_xdevap\Session object.

Beispiel #1 Connecting to a MySQL Server

<?php
try {
    
$session mysql_xdevapi\getSession("mysqlx://user:password@host");
} catch(
Exception $e) {
    die(
"Connection could not be established: " $e->getMessage());
}
 
// ... use $session
?>

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:

Beispiel #2 Creating a Schema and Collection on the MySQL Server

<?php
$schema 
$session->createSchema("test");
$collection $schema->createCollection("example");
?>

When storing data, typically json_encode() is used to encode the data into JSON, which can then be stored inside a collection.

The following example stores data into the collection we created earlier, and then retrieve parts of it again.

Beispiel #3 Storing and Retrieving Data

<?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());
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

array(4) {
  ["_id"]=>
  string(28) "00005ad66aaf0000000000000003"
  ["age"]=>
  int(39)
  ["job"]=>
  string(7) "Manager"
  ["name"]=>
  string(4) "Mike"
}

The example demonstrates that the MySQL Server adds an extra field named _id, which serves as primary key to the document.

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.

Optionally use PHP's iterators fetch multiple documents:

Beispiel #4 Fetching and Iterating Multiple Documents

<?php
$result 
$collection->find()->execute();
foreach (
$result as $doc) {
  echo 
"${doc["name"]} is a ${doc["job"]}.\n";
}
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Marco is a Programmer.
Mike is a Manager.

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Wie kann man komplexe Abfragen mit SQL-Querys In MySQLi effektiv durchführen?

In diesem MySQL-Tutorial wird erklärt, wie komplexe SQL-Abfragen in MySQLi effizient durchgeführt werden können. Wir werden uns mit verschiedenen Aspekten der Datenbankabfrage beschäftigen und spezifische Methoden kennenlernen. ...

TheMax

Autor : TheMax
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

[Wichtig] Anmeldung im Forum

Guess the hidden word using logic and deduction in the engaging game of Wordle. Wordle nyt (https://wordlenytimes.net/) ​

Geschrieben von lenytimes am 19.04.2024 19:21:45
Forum: Fragen/Vorschläge zum Forum
Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hallöchen zusammen, ich versuche derzeit unseren Excel-Spielplan in PHP zu überführen. Eigentlich bin ich auch shon fertig - wenn da nicht dies ...

Geschrieben von derH0st am 11.04.2024 15:58:37
Forum: PHP Developer Forum