faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MSSQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

30 of 45 people (67%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How can I access a text field from a database stored in MSSQL Server? It doesn't seem to get the data from the table if it is a text field!

Aug 9th, 2005 04:10
Vincent De Baere, Adrian Kubala, Felipe Almeida, Onno Benschop,


Basically mssql only returns 255 characters of a big text field. I have
no idea why. There was an answer to this problem a while back on
phpbuilder.com or something like that, I'm afraid I don't remember the
exact source. Anyways, here's a bit of code which you can use to get
around this. It might not work right off, I had to edit it a bit to
extract it from where I was using it:
function fetch_text($db,$field,$table,$ID,$IDvalue,$where="") {
	if (!$where) $where = "WHERE $ID = '$IDvalue'";
	$query = "SELECT DATALENGTH($field) AS length FROM $table $where";
	$result = mssql_query($query,$db);
	$array = mssql_fetch_array($result);
	$length = $array["length"];
	if ($length) {
		$offset = 1;
		do {
			$query = "SELECT SUBSTRING(
				$field, $offset, ".(($offset + 255 <
				$length)?255:($length-$offset)+1).")
				AS txt FROM $table $where";
			$result = mssql_query($query,$db);
			$array = mssql_fetch_array($result);
			$txt = $array["txt"];
			$return .= $txt;
			$offset += 255;
		} while ($offset < $length);
	}
	return $return;
}