php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44325 MSSQL empty string as NULL
Submitted: 2008-03-04 16:09 UTC Modified: 2008-03-05 23:53 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: alexr at oplot dot com Assigned:
Status: Not a bug Package: MSSQL related
PHP Version: 5.2.5 OS: All
Private report: No CVE-ID: None
 [2008-03-04 16:09 UTC] alexr at oplot dot com
Description:
------------
mssql_bind not correctly bind empty strings as parameter value.

I add in php_mssql.c

if(!datalen) {
   datalen = 1;
   maxlen  = 1;
}

After the 

value=(LPBYTE)Z_STRVAL_PP(var);

To fix this problem.
Please add this code to the next PHP realize.

Reproduce code:
---------------
Code 1:
<?php
$cn = mssql_connect($DBSERVER, $DBUSER, $DBPASS);
mssql_select_db($DB, $cn);
$sp = mssql_init("sp_test");
$val = 0;
mssql_bind($sp, "RETVAL", &$val, SQLINT4);
mssql_bind($sp, "@ID", "", SQLVARCHAR, false, 0);
mssql_execute($sp);
mssql_close($cn);
echo $val;
?>
Code 2:
<?php
$cn = mssql_connect($DBSERVER, $DBUSER, $DBPASS);
mssql_select_db($DB, $cn);
$sp = mssql_init("sp_test");
$val = 0;
mssql_bind($sp, "RETVAL", &$val, SQLINT4);
mssql_bind($sp, "@ID", null, SQLVARCHAR, false, 1);
mssql_execute($sp);
mssql_close($cn);
echo $val;
?>

SQL Code

CREATE PROCEDURE sp_test
    @ID varchar(255) null
AS
begin
    if @ID is NULL return 2
    else return 3
END

Expected result:
----------------
1) "3"
2) "2"

Actual result:
--------------
1) "2"
2) "2"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-04 16:20 UTC] alexr at oplot dot com
Sorry. This is duplicate of http://bugs.php.net/bug.php?id=40394 bug with fix :)
Could you please correct my code or approve it?
 [2008-03-04 17:14 UTC] alexr at oplot dot com
I'm update code :)
replace
	if ( (type==SQLVARCHAR) || (type==SQLCHAR) || (type==SQLTEXT) )	{	/* variable-length type */
		if (is_null) {
			maxlen=0;
			datalen=0;
		}
		else {
			convert_to_string_ex(var);
			datalen=Z_STRLEN_PP(var);
			value=(LPBYTE)Z_STRVAL_PP(var);
		}
	}

To
	if ( (type==SQLVARCHAR) || (type==SQLCHAR) || (type==SQLTEXT) )	{	/* variable-length type */
		if (is_null) {
			maxlen=0;
			datalen=0;
		}
		else {
			if( Z_TYPE_PP(var) == IS_NULL ) {
				maxlen=0;
				datalen=0;
			}
			else
			{
				convert_to_string_ex(var);
				datalen=Z_STRLEN_PP(var);
				value=(LPBYTE)Z_STRVAL_PP(var);
				if(!datalen) {
					datalen=1;
					if( maxlen == -1 ) maxlen=1;
				}
			}
		}
	}
 [2008-03-04 19:39 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2008-03-05 09:37 UTC] alexr at oplot dot com
I'm sorry. This bug is not fixed. This is a bug of the ntwdblib.dll. And after latest updates the bug is return.

When I bind a empty string, the stored procedure received a chr(0) char instead a empty string.

Please roll back last fixes. This bug is irrecoverable.
 [2008-03-05 23:53 UTC] iliaa@php.net
Patch reverted, thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC