Microsoft® ODBC Driver 11 for SQL Server® on RedHat Linux with PHP - gives an error when bind parameter with PDO for stored procedures

非 Y 不嫁゛ 提交于 2019-12-20 04:16:52

问题


Without any parameters it will works fine, but when parameters are there, gives the following error -

SQLSTATE[42000]: Syntax error or access violation: 8018 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid parameter 4 (''): Data type 0x23 is a deprecated large object, or LOB, but is marked as output parameter. Deprecated types are not supported as output parameters. Use current large object t (SQLExecute[8018] at /builddir/build/BUILD/php-5.3.3/ext/pdo_odbc/odbc_stmt.c:254)

This runs on Centos 6 , ODBC Driver 11 for SQL Server® - RedHat Linux , unixODBC-2.3.0 , MSSQL Server 2008 R2

Connection string :

    $con = new PDO("odbc:dsnName", 'sa','saa');
    $con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    $con->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); 

Sample stored procedure used :

    $stmt = $con->prepare( "SET NOCOUNT ON DECLARE  @return_value int

    EXEC    @return_value = [sp_insert_into_t_contact_test]
    @paravalue = ?

    SELECT  'returnV' = @return_value");
    $stmt->bindParam(1, $v1 = 5, PDO::PARAM_STR, 100);
    $stmt->execute(); 
    $return =$stmt->fetch();
    echo $return['returnV'];

Simple stored procedure to return "123"

    [dbo].[sp_insert_into_t_contact_test]
       @paravalue varchar(100)
    AS
    return 123

Additional information -

odbc.ini file -

   [dsnName]
   Driver=SQL Server Native Client 11.0
   Description=My Sample ODBC Database Connection
   Trace=Yes
   Server=192.168.2.60
   Port=1433
   Database=NSCDB_3

odbcinst.ini

  [SQL Server Native Client 11.0]
  Description=Microsoft ODBC Driver 11 for SQL Server
  Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0
  Threading=1
  UsageCount=1

回答1:


Finally I found the solution for this, Microsoft driver has develop for C or C++ application and the PDO parameter binding does not work as expected. If you pass parameters without using PDO everything works as expected. Replace with @paravalue = N'".$v1."'

Fore example - normal parameter binding - SQLBindParameter PDO parameter binding - $stmt->bindParam(1, $v1 = 5, PDO::PARAM_STR, 100);

Corrected cod snippet:

        $stmt = $con->prepare( "SET NOCOUNT ON DECLARE  @return_value int

        EXEC    @return_value = [sp_insert_into_t_contact_test]
        @paravalue = N'".$v1."'

        SELECT  'returnV' = @return_value");
        $stmt->execute(); 
        $return =$stmt->fetch();
        echo $return['returnV'];


来源:https://stackoverflow.com/questions/18441721/microsoft-odbc-driver-11-for-sql-server-on-redhat-linux-with-php-gives-an-er

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!