473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to retrieve an Oracle VARRAY

I am using PHP 4.4.4 with Oracle 10g Express Edition on Windows XP.

I am trying to emulate in Oracle the SET datatype in MySQL and the ARRAY
datatype in PostgreSQL

I have the created a user-defined datatype as follows:

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE TYPE t_fav_food IS VARRAY(10) OF NUMBER(2);
  2.  
  3. CREATE TABLE person (
  4. person_id varchar2(8) NOT NULL,
  5. first_name varchar2(20) NOT NULL,
  6. last_name varchar2(30) NOT NULL,
  7. favourite_food t_fav_food,
  8. PRIMARY KEY  (person_id)
  9. );
  10.  
If I access this table with the following code:

Expand|Select|Wrap|Line Numbers
  1. $array = array();
  2. $query = 'SELECT person_id, first_name, last_name, favourite_food FROM
  3. person';
  4. $statement = ociParse($conn, $query);
  5. $result = ociExecute($statement);
  6. while (ociFetchInto ($statement, $row,
  7. OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS)) {
  8. $array[] = array_change_key_case($row, CASE_LOWER);
  9. } // while
  10.  
I get the error "ORA-00932: inconsistent datatypes, expected CHAR got ARRAY"

I have also tried the following:

Expand|Select|Wrap|Line Numbers
  1. $array = array();
  2. $query = 'SELECT person_id, first_name, last_name, :favourite_food FROM
  3. person';
  4. $statement = ociParse($conn, $query);
  5. $fav_food = ociNewCollection($this->dbconnect, 'T_FAV_FOOD'));
  6. ociBindByName($statement, ':favourite_food', $fav_food, -1, OCI_B_SQLT_NTY);
  7. $result = ociExecute($statement);
  8. while (ociFetchInto ($statement, $row,
  9. OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS)) {
  10. $array[] = array_change_key_case($row, CASE_LOWER);
  11. } // while
  12.  
but I still get exactly the same error. The PHP manual does not give any
practical examples, and neither does the Oracle manual. I've searched the
net all day without finding anything which is remotely useful. Can anyone
help?

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
Dec 17 '06 #1
3 3779
On Sun, 17 Dec 2006 22:21:59 -0000, "Tony Marston" <to**@NOSPAM.demon.co.uk>
wrote:
>I am using PHP 4.4.4 with Oracle 10g Express Edition on Windows XP.

I am trying to emulate in Oracle the SET datatype in MySQL and the ARRAY
datatype in PostgreSQL
Bleh, but alright then.
>
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE TYPE t_fav_food IS VARRAY(10) OF NUMBER(2);
  2. CREATE TABLE person (
  3.  person_id varchar2(8) NOT NULL,
  4.  first_name varchar2(20) NOT NULL,
  5.  last_name varchar2(30) NOT NULL,
  6.  favourite_food t_fav_food,
  7.  PRIMARY KEY  (person_id)
  8. );

but I still get exactly the same error. The PHP manual does not give any
practical examples, and neither does the Oracle manual. I've searched the
net all day without finding anything which is remotely useful. Can anyone
help?
As far as I can see, SELECTing VARRAYs is not supported by PHP's oci8
extension. It has some support for collection types as binds into PL/SQL calls,
but I can't spot any way to get it to work for defines.

Consider the below that shows it works for binds, at least:

Given that the following has been run against your definitions above:
insert into person values (1, 'Alice', 'McExample', t_fav_food(1, 2));

<?php
$conn = oci_connect('test', 'test', 'xe_excession');

$array = array();
$query = 'begin select favourite_food into :favourite_food from person where
person_id = 1; end;';
$statement = oci_parse($conn, $query);

$col = oci_new_collection($conn, 'T_FAV_FOOD');
oci_bind_by_name($statement, 'FAVOURITE_FOOD', $col, -1, SQLT_NTY);

$result = ociexecute($statement);

for ($i = 0; $i < $col->size(); $i++)
{
print $col->getElem($i) . "<br>";
}
?>

Output:
1
2

I'm using the newer function names since I'm on PHP5 and using the latest oci8
extension; I don't know what state of collections support was present in the
version of oci8 bundled with 4.4.4 - you can upgrade the oci8 extension
separately from PECL (and this is strongly recommended given the large overhaul
it got in version 1.1).

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 17 '06 #2

"Andy Hassall" <an**@andyh.co.ukwrote in message
news:q1********************************@4ax.com...
On Sun, 17 Dec 2006 22:21:59 -0000, "Tony Marston"
<to**@NOSPAM.demon.co.uk>
wrote:
>>I am using PHP 4.4.4 with Oracle 10g Express Edition on Windows XP.

I am trying to emulate in Oracle the SET datatype in MySQL and the ARRAY
datatype in PostgreSQL

Bleh, but alright then.
>>
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE TYPE t_fav_food IS VARRAY(10) OF NUMBER(2);
  2. CREATE TABLE person (
  3.  person_id varchar2(8) NOT NULL,
  4.  first_name varchar2(20) NOT NULL,
  5.  last_name varchar2(30) NOT NULL,
  6.  favourite_food t_fav_food,
  7.  PRIMARY KEY  (person_id)
  8. );

but I still get exactly the same error. The PHP manual does not give any
practical examples, and neither does the Oracle manual. I've searched the
net all day without finding anything which is remotely useful. Can anyone
help?

As far as I can see, SELECTing VARRAYs is not supported by PHP's oci8
extension. It has some support for collection types as binds into PL/SQL
calls,
but I can't spot any way to get it to work for defines.

Consider the below that shows it works for binds, at least:

Given that the following has been run against your definitions above:
insert into person values (1, 'Alice', 'McExample', t_fav_food(1, 2));

<?php
$conn = oci_connect('test', 'test', 'xe_excession');

$array = array();
$query = 'begin select favourite_food into :favourite_food from person
where
person_id = 1; end;';
$statement = oci_parse($conn, $query);

$col = oci_new_collection($conn, 'T_FAV_FOOD');
oci_bind_by_name($statement, 'FAVOURITE_FOOD', $col, -1, SQLT_NTY);

$result = ociexecute($statement);

for ($i = 0; $i < $col->size(); $i++)
{
print $col->getElem($i) . "<br>";
}
?>

Output:
1
2

I'm using the newer function names since I'm on PHP5 and using the latest
oci8
extension; I don't know what state of collections support was present in
the
version of oci8 bundled with 4.4.4 - you can upgrade the oci8 extension
separately from PECL (and this is strongly recommended given the large
overhaul
it got in version 1.1).

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Unfortunately that does not work as $col>size() always returns 0, even when
I know the field is not empty.

Even if this approach did work I do not like the idea of having to retrieve
the VARRAY column independently of all the other columns.

I know that I can write into this column using the name of the user-defined
type as a function name, as in

UPDATE person SET favourite_food=T_FAV_FOOD('1','2','4','10') WHERE
person_id='FB'

so would it not be possible to write a function which would do the reverse,
i.e. convert the array into a comma-delimited string? This would then enable
me to read the table with

SELECT person_id, first_name, last_name, T_FAV_FOOD_R(favourite_food) FROM
person WHERE person_id='FB'

Hopefully that should then get around the "ORA-00932: inconsistent
datatypes, expected CHAR got ARRAY"
error. What do you think?

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
Dec 18 '06 #3
On Mon, 18 Dec 2006 11:37:15 -0000, "Tony Marston" <to**@NOSPAM.demon.co.uk>
wrote:
>Unfortunately that does not work as $col>size() always returns 0, even when
I know the field is not empty.
Works for me, as demonstrated above, but as I said YMMV depending on the
version of the oci8 extension you're using.
>Even if this approach did work I do not like the idea of having to retrieve
the VARRAY column independently of all the other columns.
Indeed, it's not suitable for your needs, just a demonstration of the limited
support the oci8 extension does have in this area.
>I know that I can write into this column using the name of the user-defined
type as a function name, as in

UPDATE person SET favourite_food=T_FAV_FOOD('1','2','4','10') WHERE
person_id='FB'

so would it not be possible to write a function which would do the reverse,
i.e. convert the array into a comma-delimited string? This would then enable
me to read the table with

SELECT person_id, first_name, last_name, T_FAV_FOOD_R(favourite_food) FROM
person WHERE person_id='FB'

Hopefully that should then get around the "ORA-00932: inconsistent
datatypes, expected CHAR got ARRAY"
error. What do you think?
I think it's an abomination ;-) but that's what I think about varrays as table
columns in the first place. Without submitting a patch to the oci8 extension to
support defining varrays, then it's probably the closest you'll get, and should
work.

Probably the main reason why you're having trouble here is that hardly anyone
uses nested tables/varray columns in Oracle in the first place; that varray
column should be a separate table. Since they're not generally used like that,
then interface support in many languages is poor (as in there's full support on
Oracle's interface side, but it's not been connected in the PHP oci8
extension). But I do understand why you're trying to do this.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tomislav Petrovic | last post by:
I have following code on my page.... $ora_tns = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=pirana)(PORT=1521)) )(CONNECT_DATA=(SID=pirana)(SERVER=DEDICATED)))"; $ora_username =...
0
by: PedroSilva | last post by:
I need to know how to get a collection (varray), basically it's an array of chars with size of 9999 positions, from oracle so I can use it as an array with php. There's very little documentation...
0
by: arvind | last post by:
Hello, We have been working on migrating oracle database to postgres for one of our client. We have a stored procedure in oracle which uses varray and I have to convert this stored procedure to...
3
by: sam.lumbroso | last post by:
I have the following object type created in Oracle (OCI8): create or replace type address as object ( address VARCHAR2(80), city VARCHAR2(80), state VARCHAR2(20), zip VARCHAR2(10) )
1
by: jpabich | last post by:
I am trying to retrieve an Oracle Time stamp with time zone information. Somewhere between Oracle and .NET, it is dropping this data. Can you tell me how to retrieve it?
0
by: Aravindkumar | last post by:
Hi My name is Aravind. I would like to know how VARRAY and NESTED TABLES can be used in Oracle Stored Procedures / Packages. Please treat this as urgent Aravind
0
debasisdas
by: debasisdas | last post by:
RESTRICTIONS ON CURSOR VARIABLES ================================= Currently, cursor variables are subject to the following restrictions: Cannot declare cursor variables in a package spec. ...
2
by: maanasa | last post by:
hi, My question is can we retrieve data from oracle in built tables like dba_users, dba_roles, dba_role_privs on to a text field using oracle forms. If yes then what're the privileges that needs to...
2
by: NewToOracle | last post by:
Hi. Here is the code I tried for the use of the varray..I am trying to dump the values into the array using for loop.. But it's giving the error "error : Trying to access uninitialized object" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.