|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26315 mssql_fectch_* functions are returning a space char on empty fields
Submitted: 2003-11-19 07:20 UTC Modified: 2007-04-06 15:46 UTC
From: webmaster at cbre dot fr Assigned:
Status: Not a bug Package: MSSQL related
PHP Version: 4.3.4 / 4.4.6 OS: Windows 2000/2003 Server
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
35 + 36 = ?
Subscribe to this entry?

 [2003-11-19 07:20 UTC] webmaster at cbre dot fr
mssql_fectch_* functions are returning a space char when selecting empty fields. 

The problem does not occur where MSSQL table fields are set to <NULL> value insteed of empty.

It was not working this way with PHP 4.3.2 and previous versions. I can't see nothing in the change log about this.

Kind regards


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-19 09:42 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

mssql extension no longer automatically trims varchar fields
 [2003-11-21 07:56 UTC] w1lz0r at barrysworld dot net
This is a bug, as there is no space in the database. The change from not auto-trimming is a good one IMO, but the behaviour of empty fields is different now.

Any character field which is empty (e.g. '') and not null will be returned as a space.

My application no longer worked correctly after the 4.3.4 change and I have to now manually do (if ($field==' ') $field='';). This is a major change from previous versions and a bug as the mssql functions should return what is IN the database. Maybe this is the reason why the rtrim was done in the past?

Please re-evaluate.
 [2003-11-21 11:33 UTC] jabberwocky at ibplanet dot com
also, instead of returning 0 it is now also returning a space if there is a 0 in the DB
 [2003-12-01 20:29 UTC] php at electricsurfer dot com
THIS IS A BUG new to 4.3.4.

Introduced by removing the auto-trimming

While the change is a good one, if '' is in the DB, mssql_fetch_* should NOT return ' '

IN DB: '' (empty string)
Returned by mssql_fetch_assoc(): ' ' (single space)
 [2004-02-26 01:47 UTC] dneilsen at iprimus dot com dot au
I too have encountered this bug with windows 2003 server using version 4.3.4 of php.

Where as previously my empty varchar fields where returned as '' they are now returned as ' ' which has caused several of my applications to fail.
 [2004-03-16 13:29 UTC] webmaster at groupphotographers dot com
THIS most DEFINITELY is a BUG! I have built a VERY LARGE DB driven PHP site. In many places, I check to see if the DB field was empty or not. Before, Fetch always returned an empty string or a NULL, it doesn't matter which because it evaluated to a FALSE, and now it returns a space character which evaluates to TRUE! This is quite nasty. I'm going to add this ME TOO comment because it hasn't been fixed yet, and it really needs to be...PLEASE!
 [2004-03-24 04:18 UTC] francois dot zietlow at ri-solution dot com
I think that is a Bug, too.
I have the same Problem.
It's not a solution to trim any value. Or??

Please write why do you think it's not a Bug.

 [2004-03-24 04:33 UTC] francois dot zietlow at ri-solution dot com
The Protocol have been changed.
Quote from Sybase Doco.

The empty string ("") is treated as a single space. In *char* and
*nchar* *not null* columns, the result is a (column-length) field of

Found on Bug Report:

Its not nice...
Just I write my own empty-Function.....

 [2004-04-23 10:25 UTC] tomasz at biznespolska dot pl
In my opinion this is bug, and is still present on version 4.3.6. This is not only problem with CHAR, and NCHAR columns but also empty VARCHAR columns returns space.

$re = mssql_fetch_row(mssql_query("SELECT CAST('' AS VARCHAR(10))"));
// the result is: -- --
// and should be: ----

Because of that I still use PHP4.3.3.
 [2004-05-24 21:23 UTC] hagen at woecht dot de
If you must make from a space to '' in your PHP script, how can you distinguish between a "true" space or "faked" space?
All '' and space in the DB are then '' (:-((()
 [2004-06-20 02:49 UTC] spam2 at rhsoft dot net
hmm this is a bug
i developed a cms first for mysql and the port for postgre with select database-type works very fine

the last two days i try to get it running with mssql/msde an nothing works with this damned spaces

if (!empty($image)) echo "<img src=\"" . $image . "\" />"; for example makes a big shit :-(
 [2004-08-18 22:33 UTC] jwhatcher at hotmail dot com
'' = ' '. Politely, this is a bug. In transact sql and query analyzer '' is seen as '' but in php '' is seen as ' '. I am using varchar fields. This surely breaks several lines of code for the empty function is now useless when selecting '' out of the database. Please reconsider and look into this item. 

Add autotrim to the ini file if this would fix the problem.
 [2007-04-06 08:48 UTC] webmaster at cbre dot fr
More than 3 years later (version 4.4.6 tested today) the bug seems to be still present.
Please, if you don't wan't to consider this issue as a bug, would it be possible to add a "mssql.trimemptystringspace" option to the mssql module in order to let developpers choose to activate or not this feature via init_set() or the php.ini file ?
It would be more efficient than triming all returned row in the PHP code.
Thanks in advance.
 [2007-04-06 15:46 UTC]
The bug is in the Microsoft library not in the PHP extension.

Use the php_dblib.dll version of the extension. It does not have the 
problem, though it will require that you update to version 5 of PHP.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 28 02:01:31 2024 UTC