tags:

views:

30

answers:

2

I am attempting to transfer a set of photos (blobs) from one table to another across databases. I'm nearly there, except for binding the photo parameter. I have the following code:

$conn_db1 = oci_pconnect('username', 'password', 'db1');
$conn_db2 = oci_pconnect('username', 'password', 'db2');

$parse_db1_select = oci_parse($conn_db1,
"SELECT
    REF PID,
    BINARY_OBJECT PHOTOGRAPH
FROM
    BLOBS");

$parse_db2_insert = oci_parse($conn_db2,
"INSERT INTO
    PHOTOGRAPHS
    (PID,
    PHOTOGRAPH)
VALUES
    (:pid,
    :photo)");    

oci_execute($parse_db1_select);

while ($row = oci_fetch_assoc($parse_db1_select)) {
    $pid = $row['PID'];
    $photo = $row['PHOTOGRAPH'];

    oci_bind_by_name($parse_db2_insert, ':pid', $pid, -1, OCI_B_INT);

    // This line causes an error
    oci_bind_by_name($parse_db2_insert, ':photo', $photo, -1, OCI_B_BLOB);

    oci_execute($parse_db2_insert);
}

oci_close($db1);
oci_close($db2);

But I get the following error, on the error line commented above:

Warning: oci_execute() [function.oci-execute]: ORA-03113: end-of-file on communication channel Process ID: 0 Session ID: 790 Serial number: 118 

Does anyone know the right way to do this?

Problem Solved

With a bit of internet searching I found a working solution. I changed the insert SQL to:

$parse_db2_insert = oci_parse($conn_db2,
"INSERT INTO
    PHOTOGRAPHS
    (P_ID,
    PHOTOGRAPH)
VALUES
    (:pid,
    EMPTY_BLOB())
RETURNING PHOTOGRAPH INTO :photo");

Then changed the While loop thus:

while ($row = oci_fetch_assoc($parse_db1_select)) {
    $pid = $row['PID'];
    $photo = $row['PHOTOGRAPH'];

    oci_bind_by_name($parse_db2_insert, ':pid', $pid);

    $new_lob = oci_new_descriptor($conn_unite, OCI_D_LOB);
    oci_bind_by_name($parse_db2_insert, ':photo', $new_lob, -1, OCI_B_BLOB);

    oci_execute($parse_db2_insert, OCI_DEFAULT);

    $new_lob->save($photo->load());
    oci_commit($conn_unite);
}

Peculiar, but true.

+1  A: 
oci_bind_by_name($parse_db_insert, ':photo', $photo, -1, OCI_B_BLOB);

You haven't defined a $parse_db_insert variable.

I guess it's just this mistake.

PART 2

This ORACLE error is returned when the connection has been established but then failed like in a timeout (full explanation here: http://www.dba-oracle.com/m_ora_03113_end_of_file_on_communications_channel.htm)

First, are you sure the two variables $pid and $photo contain actual values? Second, in the PHP manual under oci_bind_by_name states that:

A bind call tells Oracle which memory address to read data from. For IN binds that address needs to contain valid data when oci_execute() is called. This means that the variable bound must remain in scope until execution. If it doesn't, unexpected results or errors such as "ORA-01460: unimplemented or unreasonable conversion requested" may occur. For OUT binds one symptom is no value being set in the PHP variable.

Full text here: http://php.net/manual/en/function.oci-bind-by-name.php

Maybe you just got the binding wrong or your variable is empty.

Lex
Thanks for that. I sanitised my code from the original, it was just a typo. I've corrected it. The error still stands.
Jonathan Swift
A: 

No - the error comes from the following line.

You don't check your previous oci calls to see if they returned a valid result - you'll get this error here if the connection failed or timed out.

symcbean