Installieren und konfigurieren Sie die Oracle -Datenbank.
Installieren Sie PHP- und Oracle -Datenbanktreiber. Sie können PECL verwenden oder manuell installieren.
Erstellen Sie eine Tabelle mit JSON- und XML -Typspalten in einer Oracle -Datenbank. Hier ist eine einfache Beispieltabellenstruktur:
Tabelle erstellen my_table ( ID -Nummer Primärschlüssel, JSON_DATA CLOB, xml_data xmlType );
$ conn = oci_connect ('Benutzername', 'Passwort', 'localhost/orcl'); if (! $ conn) { $ e = oci_error (); Trigger_error (htmlentities ($ e ['message'], ent_quotes), e_user_error); }
Als nächstes können wir die JSON -Daten einfügen:
$ json_data = '{"Name": "John", "Alter": 30, "City": "New York"}'; $ stmt = oci_parse ($ conn, 'in my_table (id, json_data) Werte (1,: json_data)'); oci_bind_by_name ($ stmt, ': json_data', $ json_data); oci_execute ($ stmt); oci_commit ($ conn);
Wenn Sie JSON -Daten abfragen, können Sie den folgenden Code verwenden:
$ stmt = oci_parse ($ conn, 'select json_data von my_table wobei id = 1'); oci_execute ($ stmt); $ json_data = oci_fetch_assoc ($ stmt) ['json_data']; $ data = json_decode ($ json_data, true); echo $ data ['name']; // JOHN ausgeben
$ xml_data = '<root><person><name> John</name><age> 30</age><city> New York</city></person></root> '; $ stmt = oci_parse ($ conn, 'in my_table (id, xml_data) Werte (2, xmlType (: xml_data))')); oci_bind_by_name ($ stmt, ': xml_data', $ xml_data); oci_execute ($ stmt); oci_commit ($ conn);
Wenn Sie XML -Daten abfragen, können Sie den folgenden Code verwenden:
$ stmt = oci_parse ($ conn, 'select xml_data.getClobval () als xml_data von my_table wobei id = 2'); oci_execute ($ stmt); $ xml_data = oci_fetch_assoc ($ stmt) ['xml_data']; $ xml = simplexml_load_string ($ xml_data); echo $ xml-> Person-> Name; // JOHN ausgeben