none
mssql_connect() is now sqlsrv_connect() or what am I missing?

    Question

  • All of our prior code uses PEAR::DB, which makes use of mssql_connect in it's driver.

    I am running 5.3.0 and using the 1.1 driver php_sqlsrv_53_ts_vc6.dll on my WinXP development machine.

    I have the proper sql client installed, as well as sql server 2005 running.

    I'm able to connect to a test database using sqlserv_connect() , but mssql_connect throws an exception stating the function does not exist.

    Am I to understand that the new driver isn't backwards compatible or am I missing something that should be obvious?

    Thanks for any insight,
    Justin

    ps - My phpinfo() shows:

    sqlsrv

    sqlsrv support enabled
    Directive Local Value Master Value
    sqlsrv.LogSeverity 0 0
    sqlsrv.LogSubsystems 0 0
    sqlsrv.WarningsReturnAsErrors On On


    Wednesday, September 02, 2009 6:29 PM

Answers

  • The SQL Server driver for PHP developed and supported by Microsoft is “sqlsrv” and not “mssql”. The two drivers are similar, but not compatible. IA quick glance through the APIs list should reveal the differences. For instance, mssql has a “bind” function, while “sqlsrv” has not (as it implements implicit binding with prepared statements). In summary, there is no (backward) compatibility between the two drivers.

    Additionally, “mssql” was dropped from PHP 5.3 distribution. Since the “php_mssql.dll” is no longer deployed with PHP 5.3, all calls to “mssql_” API are expected to fail.


    Serban Iliescu - MSFT
    Thursday, September 03, 2009 3:26 PM
    Answerer

All replies

  • Rather than worry about why, or waste time searching for answers, I just created a functional PEAR::DB driver for sqlsrv.

    It's mostly a copy of the mssql db driver, with the new sqlserv calls.

    If anyone else has this issue, feel free to grab it:
    http://www.tehuber.com/code/sqlsrvdriver.tar

    ps - I only tested the stuff I needed, basic query, insert, updated, etc, I wouldn't call it production ready.
    Wednesday, September 02, 2009 9:48 PM
  • The SQL Server driver for PHP developed and supported by Microsoft is “sqlsrv” and not “mssql”. The two drivers are similar, but not compatible. IA quick glance through the APIs list should reveal the differences. For instance, mssql has a “bind” function, while “sqlsrv” has not (as it implements implicit binding with prepared statements). In summary, there is no (backward) compatibility between the two drivers.

    Additionally, “mssql” was dropped from PHP 5.3 distribution. Since the “php_mssql.dll” is no longer deployed with PHP 5.3, all calls to “mssql_” API are expected to fail.


    Serban Iliescu - MSFT
    Thursday, September 03, 2009 3:26 PM
    Answerer
  • Justin,

    Thank you for the driver, I found it extremely helpful for getting sqlsrv to work with PEAR.

    That said, I had to make a few modifications to get it to work:

    --------------------

    At the end of the connect function, you make a call to sqlsrv_get_last_message(), which is invalid.  I had to replace the code with:

    $errors = sqlsrv_errors();
    $message = $errors[0]['message'];

    return $this->raiseError(DB_ERROR_CONNECT_FAILED,null, null, null,$message);

    --------------------

    You removed a few instances where it checked for DB_ERROR_NODBSELECTED, presumably because selecting databases does not work the same way in sqlsrv.  However, there were a few places where you did not remove them.

    --------------------

    Lastly (and perhaps most importantly), in the tableinfo() function, you access the field data using methods like sqlsrv_name_field, which do not exist.  Instead, you need to access the field metadata, by first grabbing it when you perform a query, and then referencing the fields later using the provided arrays.

    I resolved this problem by:

    1. Adding a metadata variable

    2. in simpleQuery(), adding the following line once you've grabbed the result:

    $this->metadata = sqlsrv_field_metadata($result);

    3. Accessing this stored metadata in the tableinfo() function, like so:

    for ($i = 0; $i < $count; $i++) {
                if ($got_string) {
                                $flags = $this->_sqlsrv_field_flags($result,
                                        sqlsrv_field_name($id, $i));
                                if (DB::isError($flags)) {
                                    return $flags;
                                }
                            } else {
                                $flags = '';
                }

    foreach($this->metadata as $name => $fields)
    {
              $res[$i] = array(
              'table' => $got_string ? $case_func($result) : '',
              'name'  => $fields['Name'],
              'type'  => $fields['Type'],
              'len'   => $fields['Size'],
              'flags' => $flags,
              );
    }

    -----------------------

    Despite these issues, your driver worked as a great starting off point for me to come up with a solution, so thank you for that.

    Thursday, November 11, 2010 4:03 PM