Current Location: Home> Latest Articles> How to Work with JSON and XML Data in Oracle Database Using PHP

How to Work with JSON and XML Data in Oracle Database Using PHP

M66 2025-06-17

Introduction

In web development, handling unstructured data, such as JSON and XML, has become increasingly essential. Oracle, as a widely-used relational database system, offers powerful capabilities to store and manipulate these types of data. In this article, we will focus on how to connect and work with JSON and XML data in an Oracle database using PHP, and we will provide code examples to help developers understand and implement these techniques.

1. Preparations

Before starting, please ensure that you have completed the following preparations:
  1. Install and configure the Oracle database.

  2. Install PHP and the Oracle database driver. You can do this via PECL or by manually installing the driver.

  3. 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
);

2. Working with JSON Data in PHP

When working with JSON data, the first step is to connect to the Oracle database:
$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

3. Working with XML Data in PHP

To work with XML data, first insert the XML data:
$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

Conclusion

This article explained how to work with JSON and XML data in Oracle Database using PHP. By preparing the database environment and creating the appropriate table structures, you can easily insert and query JSON and XML data using PHP and Oracle. We hope the code examples provided in this article will help you better understand how to apply these techniques in real-world development.
  • Related Tags:

    JSON