By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

ODBC and storing binary data (specially BLOB)

P: n/a
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?

Nov 30 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.