For Sybase users (this probably applies to MS-SQL Server as well) who are using ODBC:
I was using the same code as mizmerize, but I was getting truncated data back from the server (at the 32kb mark) when selecting data with the image datatype. My Sybase server has a @@textsize property of 2Gb, which should be plenty. But apparently, the php ODBC driver resets this to 32Kb when a connection is made, and then sets it back to 2Gb after. The solution is to do a query:
<?php odbc_exec($connH, "set textsize 131072"); ?>
immediately before your main query, in mizmerize's code. That should override the default setting.