Simple question on arrays and sql

Given that I have a 2D array in php, is there a sql syntax to insert the
entire array at once?

For example if I retrieve a set of data from one table in the database
with keys A, B and C, and suppose there are 100 records, and I build an
array $arr consisting of

$arr[$i]['A'], $arr[$i]['B'], and $arr[$i]['C']

where $i goes from 0 to 99,

is there a syntax to insert the entire $arr array at one time into
fields A, B, and C of a new table?

I really would like to avoid having 100 sql insert calls.
BTW, this is for an Oracle DB.

In the past I have only had to insert a few records at a time, so i
simply did multiple inserts. Of course for Oracle I could do the same
and only do a commit after all are done.
sheldonlg [ Mo, 31 März 2008 17:22 ] [ ID #1931751 ]

Re: Simple question on arrays and sql

Captain Paralytic wrote:
> On 31 Mar, 16:22, sheldonlg <sheldonlg> wrote:
>> Given that I have a 2D array in php, is there a sql syntax to insert the
>> entire array at once?
>>
>> For example if I retrieve a set of data from one table in the database
>> with keys A, B and C, and suppose there are 100 records, and I build an
>> array $arr consisting of
>>
>> $arr[$i]['A'], $arr[$i]['B'], and $arr[$i]['C']
>>
>> where $i goes from 0 to 99,
>>
>> is there a syntax to insert the entire $arr array at one time into
>> fields A, B, and C of a new table?
>>
>> I really would like to avoid having 100 sql insert calls.
>> BTW, this is for an Oracle DB.
>>
>> In the past I have only had to insert a few records at a time, so i
>> simply did multiple inserts. Of course for Oracle I could do the same
>> and only do a commit after all are done.
>
> Why not use an INSERT ... SELECT syntax and do it all in SQL?

Sometimes the most obvious solution is the last one you see. This will
do it:

INSERT INTO AA (A, B, C) SELECT A, B, C FROM BB WHERE blahblahblah

Thank you
sheldonlg [ Mo, 31 März 2008 17:41 ] [ ID #1931759 ]

Re: Simple question on arrays and sql

On 31 Mar, 16:22, sheldonlg <sheldonlg> wrote:
> Given that I have a 2D array in php, is there a sql syntax to insert the
> entire array at once?
>
> For example if I retrieve a set of data from one table in the database
> with keys A, B and C, and suppose there are 100 records, and I build an
> array $arr consisting of
>
> $arr[$i]['A'], $arr[$i]['B'], and $arr[$i]['C']
>
> where $i goes from 0 to 99,
>
> is there a syntax to insert the entire $arr array at one time into
> fields A, B, and C of a new table?
>
> I really would like to avoid having 100 sql insert calls.
> BTW, this is for an Oracle DB.
>
> In the past I have only had to insert a few records at a time, so i
> simply did multiple inserts. Of course for Oracle I could do the same
> and only do a commit after all are done.

Why not use an INSERT ... SELECT syntax and do it all in SQL?
Captain Paralytic [ Mo, 31 März 2008 17:26 ] [ ID #1933060 ]
PHP » comp.lang.php » Simple question on arrays and sql

Vorheriges Thema: Is there a difference between Zend Extension and PHP Extensions
Nächstes Thema: Re: PHP Multi-threading