Nicolas Paz Nicolas Paz - 4 months ago 8
PHP Question

Use of undefined constant SQLSRV_SQLTYPE_VARCHAR - assumed 'SQLSRV_SQLTYPE_VARCHAR

I can't get to execute a sp with output parameters with SQL Server 2014, PHP 7 and Apache 2.4.17.

This is the PHP:

$nombreUsuario = "DrBrahim";
$contraseƱa = "123456";
$resultado = "dummie";
$datosBienvenida = "dummie";

$sql = "{CALL LOGINv2 (?, ?, ?, ?)}";
$params = array(
array($nombreUsuario, SQLSRV_PARAM_IN),
array($contraseƱa, SQLSRV_PARAM_IN),
array(&$resultado, SQLSRV_PARAM_OUT, null, SQLSRV_SQLTYPE_VARCHAR),
array(&$datosBienvenida, SQLSRV_PARAM_OUT, null, SQLSRV_SQLTYPE_VARCHAR),
);
$stmt = sqlsrv_query($conn, $sql, $params);

if($stmt === false){
echo 'No se pudo ejecutar la consulta:<br>';
var_dump(sqlsrv_errors());

die(print_r(sqlsrv_errors(), true));
}
else{
sqlsrv_next_result($stmt);
echo $resultado . "<br>Resultado: " . $resultado .
"<br>Datos de bienvenida: " . $datosBienvenida;
}


I get this:


Notice: Use of undefined constant SQLSRV_PHPTYPE_STRING - assumed
'SQLSRV_PHPTYPE_STRING


And this:


An invalid PHP type for parameter 3 was specified.


I've been through the following questions and references (and more) and none of them help:



Note: when i try this using WebMatrix i only see the second message.

Answer

From php.net:

SQLSRV_PHPTYPE_STRING (integer)
Specifies a string PHP data type. This constant works like a function and accepts an encoding constant.

The constant SQLSRV_PHPTYPE_STRING is to be treated as a function as you must pass an encoding constant for it to work. For example:

array(&$resultado, SQLSRV_PARAM_OUT, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR)

Encoding constants and more info on PHPTYPE constants can be found here.


From above link:

When you use SQLSRV_PHPTYPE_STREAM or SQLSRV_PHPTYPE_STRING, the encoding must be specified. If no parameter is supplied, an error will be returned.


Edit
As the question title actually asks about SQLSRV_SQLTYPE_VARCHAR and not SQLSRV_PHPTYPE_STRING I should point out that the same is true for the former:

SQLSRV_SQLTYPE_VARCHAR (integer)
Describes the varchar SQL Server data type. This constant works like a function and accepts a single parameter indicating the character count.

So with this you would add a parameter that represents the max character count for that field, just as you would in SQL. So something like:

SQLSRV_SQLTYPE_VARCHAR(512)

Where 512 is an integer between 1-8000.

For when you use varchar(MAX) in SQL you would use:

SQLSRV_SQLTYPE_VARCHAR("max")
Comments