Vscode-intelephense: Undefined constant 'SQLSRV_SQLTYPE_VARCHAR'

Created on 14 Feb 2020  路  8Comments  路  Source: bmewburn/vscode-intelephense

Describe the bug
Intelephense incorrectly states that the Microsoft SQL Server constants are undefined.

More information about these constants can be found at https://docs.microsoft.com/en-us/sql/connect/php/constants-microsoft-drivers-for-php-for-sql-server?view=sql-server-ver15

To Reproduce
First ensure that the php_sqlsrv.dll extension is loaded in IIS.

echo SQLSRV_SQLTYPE_VARCHAR;

Expected behavior
I don't expect Intelephense to identify the constant as undefined.

Platform and version
Windows 10 Pro or Windows Server 2016
Intelephense 1.3.10
PHP 7.3.10

Most helpful comment

@daveherman71 Please try adding sqlsrv into intelephense.stubs setting.

@bmewburn #4 :eyes:

All 8 comments

@daveherman71 Please try adding sqlsrv into intelephense.stubs setting.

@bmewburn #4 :eyes:

Appears that SQLSRV_SQLTYPE_VARCHAR is defined as a function in the stubs. I'm unsure if this is correct or not.

I had already added sqlsrv and pdo_sqlsrv to intelephense.stubs. This is evidenced by the fact that other constants such as SQLSRV_SQLTYPE_INT do not get highlighted as undefined.

You have a point about the definition being a function though as the varchar definition in SQL usually requires that a $charCount parameter be provided, however footnote 5 in the Microsoft documentation suggests that for type comparison you can still use the constant without the $charCount parameter.

Consider the following code snippet:

<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"AdventureWorks", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
   die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT * FROM Table_1";
$stmt = sqlsrv_prepare( $conn, $sql );

foreach( sqlsrv_field_metadata( $stmt ) as $fieldMetadata ) {
    if ($fieldMetadata["Type"] === SQLSRV_SQLTYPE_VARCHAR) {
       echo $fieldMetadata["Name"] . " is of type varchar<br />";
    }
    else if ($fieldMetadata["Type"] === SQLSRV_SQLTYPE_INT) {
       echo $fieldMetadata["Name"] . " is of type int<br />";
    }
      echo "<br />";
}
?>

In this example Intelephense reports that only SQLSRV_SQLTYPE_VARCHAR is an undefined constant,

If it is a constant then it will need an entry upstream in the phpstorm stubs https://github.com/JetBrains/phpstorm-stubs/blob/master/sqlsrv/sqlsrv.php

@KapitanOczywisty I have updated the sqlsrv.php stub to include the missing SQLTYPE Constants. Please can you include it in the repository.

sqlsrv.zip

@daveherman71 Nice, I'll make PR in a moment

merged

@KapitanOczywisty @bmewburn One fell through the cracks.

I have attached an updated zip which includes SQLSRV_SQLTYPE_BINARY.

sqlsrv.zip

Was this page helpful?
0 / 5 - 0 ratings

Related issues

steven7mwesigwa picture steven7mwesigwa  路  4Comments

9brada6 picture 9brada6  路  3Comments

ottopic picture ottopic  路  3Comments

ghnp5 picture ghnp5  路  3Comments

pseudoanime picture pseudoanime  路  3Comments