php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45551 Wrong result when selecting from varchar field
Submitted: 2008-07-18 06:33 UTC Modified: 2008-10-06 22:30 UTC
Votes:5
Avg. Score:5.0 ± 0.0
Reproduced:5 of 5 (100.0%)
Same Version:4 (80.0%)
Same OS:4 (80.0%)
From: brack at wjp dot de Assigned:
Status: Not a bug Package: MSSQL related
PHP Version: 5.2.6 OS: Windows Vista Business
Private report: No CVE-ID: None
 [2008-07-18 06:33 UTC] brack at wjp dot de
Description:
------------
On SQL Server 2005 when I execute a SELECT query to retrieve the content of a VARCHAR field with a specified length greater than 255 and less than MAX, the resulting value is truncated at a length of 255 characters.

Reproduce code:
---------------
For testing purposes, I used a table created by the following SQL code:

CREATE TABLE [TESTTABLE](
	[id] [int] NOT NULL,
	[testvalue] [varchar](1000) NULL,
 CONSTRAINT [PK_TESTTABLE] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

PHP test script code:

$conn = mssql_connect('localhost', 'testuser', 'testpassword');
mssql_select_db('testdb');
$string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
echo strlen($string) . '<br>';
mssql_query('INSERT INTO TESTTABLE (id, testvalue) VALUES (1, \'' . $string . '\')');
$result = mssql_query('SELECT testvalue FROM TESTTABLE WHERE id=1');
echo(strlen(mssql_result($result, 0, testvalue')));

Expected result:
----------------
Expected output of the PHP test script:

396
396

Actual result:
--------------
Actual output of the PHP test script:

255
396

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-18 08:23 UTC] brack at wjp dot de
Same problem occurs on MS SQL Server 2000!
 [2008-10-06 22:07 UTC] felipe@php.net
Your example looks wrong then, because the first output is produced by: echo strlen($string).
 [2008-10-06 22:30 UTC] fmk@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

The Microsoft library used to connect to MSSQL databases does not support varchar columns longer than 255 bytes.

Use the freetds version of the extension (php_dblib.dll) instead.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Sep 27 21:05:53 2022 UTC