ODBC and storing binary data (specially BLOB)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wescotte

    ODBC and storing binary data (specially BLOB)

    I'm abit confused on how to work with binary data with an ODBC
    connection (My database is DB2 btw)

    Say I have a table like
    CREATE TABLE EJWLIB.BLOBTEST (
    ID NUMERIC(5) NOT NULL,
    FILENAME VARCHAR(128) NOT NULL,
    BINARY BLOB(2M) )

    Now I (I assume this is the correct method) insert data in the
    following method

    $MyInputStream = fopen($MyFilena me, "rb");
    $binary_data = fread($MyInputS tream, filzesize($MyFi lename));
    // Do I need to check to make sure the file is not greater than the
    alloted 2M or will the SQl statement just fail if I try to insert
    something larger?

    $query = "INSERT INTO $MYTABLE VALUES ($RECORD_ID, '$MyFilename', ?)";
    $prepared_sql = odbc_prepare($M yConnection, $query);

    $result=odbc_ex ecute($prepared _sql, array("$binary_ data");

    // $result never seems to be false even if I try to insert more than 2M
    of binary data
    // When I attempt to insert more than 2M it seems to simply skip
    inserting the entire row itself but doesn't display any error message
    if ($result === false)
    echo "Failed SQL statement $query " . odbc_errormsg() ;



    Now, when I want to retrieve my binary data I do so like

    $query = "SELECT * FROM $MYTABLE WHERE ID = ?";
    $prepared_sql = odbc_prepare($M yConnection, $query);
    $result = odbc_execute($p repared_sql, array("$RECORD_ ID"));

    while (odbc_fetch_row ($result)) {
    $filename = odbc_result($re sult, "FILENAME") ;
    $OutputStream= fopen($filename , "wb");

    $binary = odbc_result($re sult, "BINARY");
    fwrite($OutputS tream,$binary);

    fclose($OutputS tream);
    }

    The other issue I have is the max amount of binary data I seem to be
    able to retrieve is 4K. Any binary data i insert with the above method
    when I attempt to retrieve it I get the correct resutls however I only
    get the first 4K (or the entire file if it was originally under 4K).
    Any ideas?

Working...