|
Posted by Wescotte on 10/29/38 11:33
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($MyFilename, "rb");
$binary_data = fread($MyInputStream, filzesize($MyFilename));
// 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($MyConnection, $query);
$result=odbc_execute($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($MyConnection, $query);
$result = odbc_execute($prepared_sql, array("$RECORD_ID"));
while (odbc_fetch_row($result)) {
$filename = odbc_result($result, "FILENAME");
$OutputStream= fopen($filename, "wb");
$binary = odbc_result($result, "BINARY");
fwrite($OutputStream,$binary);
fclose($OutputStream);
}
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?
[Back to original message]
|