Oracleデータベースをインストールして構成します。
PHPおよびOracleデータベースドライバーをインストールします。 PECLを使用するか、手動でインストールできます。
OracleデータベースにJSONとXMLタイプの列を含むテーブルを作成します。これがテーブル構造の簡単な例です。
テーブルmy_tableを作成します( ID番号プライマリキー、 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); }
次に、JSONデータを挿入できます。
$ json_data = '{"name": "john"、 "age":30、 "city": "New York"}'; $ stmt = oci_parse($ conn、 'my_table(id、json_data)values(1、:json_data)'); oci_bind_by_name($ stmt、 ':json_data'、$ json_data); oci_execute($ stmt); oci_commit($ conn);
JSONデータを照会するときは、次のコードを使用できます。
$ stmt = oci_parse($ conn、 'my_table 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']; // Johnを出力します
$ xml_data = '<root><person><name>ジョン</name><age>30</age><city>ニューヨーク</city></person></root>'; $ stmt = oci_parse($ conn、 '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);
XMLデータを照会するときは、次のコードを使用できます。
$ 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; // Johnを出力します