Mufasa10
Mufasa10

Reputation: 35

PHP + MSSQL - Value being trimmed to 255 chars when retrieving from DB

The problem is following. When I retrieve data from a specific field via PHP it's being trimmed to 255 characters. Despite the fact it is longer in DB.

$query_Result = mssql_query("SELECT comment FROM commments WHERE id = 200");
$query_Data = mssql_fetch_assoc($query_Result);
echo strlen($query_Data['comment']);

The story is following. The DB was created before me and the length of 'comment' field was short (maybe even 255 chars). I extended it:

ALTER TABLE commments 
ALTER COLUMN comment VARCHAR(1000) NULL

Should I have done something else? The value is stored normally - full length. But output is shortened to 255 chars.

Upvotes: 0

Views: 316

Answers (2)

Satya
Satya

Reputation: 8881

if you fetch varchar fields larger than 255 letters the result will be cut off at 255 letters. To prevent this you have to do a CONVERT(TEXT,data_field) for this fields in your select clause. so your code should be essentially

$query_Result = mssql_query("SELECT CONVERT(TEXT,comment) FROM commments WHERE id = 200");
$query_Data = mssql_fetch_assoc($query_Result);
echo strlen($query_Data['comment']);

Upvotes: 3

danleyb2
danleyb2

Reputation: 1068

Before Mysql version 5.0.3 Varchar datatype can store 255 character, but from 5.0.3 it can be store 65,535 characters. BUT it has a limitation of maximum row size of 65,535 bytes. It means including all columns it must not be more than 65,535 bytes. Check your Mysql version and use TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT to store large texts

Upvotes: 0

Related Questions