note 94364 added to function.odbc-result

From: Date: Fri, 30 Oct 2009 11:24:50 +0000
Subject: note 94364 added to function.odbc-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-162279@lists.php.net to get a copy of this message
The odbc_result() function has an apparently little known feature that comes in very handy when retrieving large text or binary fields. Consider this code snippet to retrieve a large value into a temporary file (error testing left out for clarity): <?php $con=odbc_connect(...); $query="set textsize 2147483647 ". "select someLargeField from ..."; $resultset=odbc_exec($con,$query); odbc_binmode($resultset,ODBC_BINMODE_RETURN); odbc_longreadlen($resultset,4096); while (odbc_fetch_row($resultset)) { $fileHandle=fopen('/tmp/myfile','wb'); while (($chunk=odbc_result($resultset,'someLargeField'))!==false) { fwrite($fileHandle,$chunk); } fclose($fileHandle); //do something with the file } ?> There are a few key points to make it work: - Make sure to include "set textsize 2147483647 " in your query. Without it, MSSQL truncates large values to 4096 bytes by default. Other databases may have a similar small limit. - The odbc_binmode() and odbc_longreadlen() calls are just for safety. Their defaults in php.ini are usually correct. - Each odbc_result() call on a large field apparently returns the next chunk. Of course you have to read all chunks before the next odbc_fetch_row() call. - When all of the large value has been read, odbc_result() will return false. Note: Using mssql_ instead of odbc_ functions is not a viable alternative. They don't work with chunks (AFAIK) and even crash php (overflow, segmentation fault) when the query result contains a large value. ---- Server IP: 213.249.64.162 Probable Submitter: 213.197.212.81 ---- Manual Page -- http://www.php.net/manual/en/function.odbc-result.php Edit -- https://master.php.net/note/edit/94364 Del: integrated -- https://master.php.net/note/delete/94364/integrated Del: useless -- https://master.php.net/note/delete/94364/useless Del: bad code -- https://master.php.net/note/delete/94364/bad+code Del: spam -- https://master.php.net/note/delete/94364/spam Del: non-english -- https://master.php.net/note/delete/94364/non-english Del: in docs -- https://master.php.net/note/delete/94364/in+docs Del: other reasons-- https://master.php.net/note/delete/94364 Reject -- https://master.php.net/note/reject/94364 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162279) next »