Install and configure the Oracle database.
Install PHP and the Oracle database driver. You can do this via PECL or by manually installing the driver.
Create a table in the Oracle database with columns of JSON and XML types. Here is an example table structure:
CREATE TABLE my_table ( id NUMBER PRIMARY KEY, json_data CLOB, xml_data XMLTYPE );
$conn = oci_connect('username', 'password', 'localhost/orcl'); if (!$conn) { $e = oci_error(); trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); }
Next, we can insert JSON data:
$json_data = '{"name": "John", "age": 30, "city": "New York"}'; $stmt = oci_parse($conn, 'INSERT INTO my_table (id, json_data) VALUES (1, :json_data)'); oci_bind_by_name($stmt, ':json_data', $json_data); oci_execute($stmt); oci_commit($conn);
To query JSON data, you can use the following code:
$stmt = oci_parse($conn, 'SELECT json_data FROM my_table WHERE id = 1'); oci_execute($stmt); $json_data = oci_fetch_assoc($stmt)['JSON_DATA']; $data = json_decode($json_data, true); echo $data['name']; // Outputs John
$xml_data = '<root><person><name>John</name><age>30</age><city>New York</city></person></root>'; $stmt = oci_parse($conn, 'INSERT INTO my_table (id, xml_data) VALUES (2, XMLTYPE(:xml_data))'); oci_bind_by_name($stmt, ':xml_data', $xml_data); oci_execute($stmt); oci_commit($conn);
To query XML data, use the following code:
$stmt = oci_parse($conn, 'SELECT xml_data.getClobVal() AS xml_data FROM my_table WHERE id = 2'); oci_execute($stmt); $xml_data = oci_fetch_assoc($stmt)['XML_DATA']; $xml = simplexml_load_string($xml_data); echo $xml->person->name; // Outputs John
Related Tags:
JSON