Viewed   952 times

I've never used php before and am trying to connect to a SQL Server 2008 instance on a Windows machine running IIS7 and PHP5.3.

I have downloaded and installed SQLSRV30.EXE from here in C:Program Files (x86)PHPext added this to C:Program Files (x86)PHPphp.ini:

extension=php_sqlsrv_53_nts.dll

Then restarted the entire server. I still get fatal errors in my log file saying:

PHP Fatal error:  Call to undefined function mssql_connect()

What do I need to do to connect to Microsoft SQL Server 2008 from PHP 5.3 running on IIS7/Windows Server 2008? I'm sure it's something really dumb that I'm missing...

FULL PHPINFO --> http://demo.mandibleweb.com/zapified/hello.php

phpinfo():

PHP Version 5.3.10





System 

Windows NT MWD001 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586 



Build Date 

Feb 2 2012 20:10:58 



Compiler 

MSVC9 (Visual C++ 2008) 



Architecture 

x86 



Configure Command 

cscript /nologo configure.js "--enable-snapshot-build" "--enable-debug-pack" "--disable-zts" "--disable-isapi" "--disable-nsapi" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=C:php-sdkoracleinstantclient10sdk,shared" "--with-oci8=C:php-sdkoracleinstantclient10sdk,shared" "--with-oci8-11g=C:php-sdkoracleinstantclient11sdk,shared" "--with-enchant=shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet" "--with-mcrypt=static" "--disable-static-analyze" 



Server API 

CGI/FastCGI 



Virtual Directory Support 

disabled 



Configuration File (php.ini) Path 

C:Windows 



Loaded Configuration File 

C:Program Files (x86)PHPphp.ini 

 Answers

1

I have just tried to install that extension on my dev server.

First, make sure that the extension is correctly enabled. Your phpinfo() output doesn't seem complete.

If it is indeed installed properly, your phpinfo() should have a section that looks like this:

If you do not get that section in your phpinfo(). Make sure that you are using the right version. There are both non-thread-safe and thread-safe versions of the extension.

Finally, check your extension_dir setting. By default it's this: extension_dir = "ext", for most of the time it works fine, but if it doesn't try: extension_dir = "C:PHPext".

===========================================================================

EDIT given new info:

You are using the wrong function. mssql_connect() is part of the Mssql extension. You are using microsoft's extension, so use sqlsrv_connect(), for the API for the microsoft driver, look at SQLSRV_Help.chm which should be extracted to your ext directory when you extracted the extension.

Thursday, October 20, 2022
2

No. Fatal errors are fatal. Even if you were to write your own error handler or use the @ error suppression operator, E_FATAL errors will still cause the script to halt execution.

The only way to handle this is to use function_exists() (and possibly is_callable() for good measure) as in your example above.

It's always a better idea to code defensively around a potential (probable?) error than it is to just let the error happen and deal with it later anyway.

EDIT - php7 has changed this behavior, and undefined functions/methods are catchable exceptions.

Wednesday, November 2, 2022
 
mage_xy
 
1

Microsoft SQL Server support and extensions are not shipped with PHP anymore. To connect PHP with SQL Server you need Microsoft Drivers for PHP for SQL Server:

The Microsoft Drivers for PHP for SQL Server is a PHP 5 extension that provides data access to SQL Server 2005 and later versions including SQL Azure.

The download page for drivers lists four versions to choose from. Consult the System Requirements page in order to determine which version you need. You must install the correct driver for your Operating System+SQL Server+PHP version combination.

You will also need one of these depending on which driver you choose (these must be installed separately):

  • SQL Server Native Client
  • Microsoft ODBC Driver 11 for SQL Server

The installer simply extracts the files in the specified location. You must the files to PHP extensions directory. The files are PHP extensions. The file names indicate:

  • nts/ts -- should be used with non thread safe/thread safe version of PHP
  • 54/55/56... -- should be used with PHP version 5.4, 5.5, 5.6, ...
  • sqlsrv/pdo_sqlsrv -- provides procedural interface/PDO interface

Enable the extensions in php.ini file making sure that thread safetyness and PHP versions match.

Check phpinfo to see if extensions are loaded. Test sqlsrv_connect("servername") and new PDO("sqlsrv:server=servername") to make sure the extensions are working.

Saturday, August 20, 2022
3

It happens because you are declaring the function after trying to call it:

tally($_POST[$qvar]);// LINE THAT CAUSES ERROR

function tally ($question) // TALLY FUNCTION DECLARATION COMES LATER

I would put either before if(isset($_POST['submit'])) { or as the first thing inside.

   if (!function_exists('tally')) {
   function tally ($question) // TALLY FUNCTION
       {   
            global $nowval;
            $nowval[$question]++;
       }   
  }

$this->tally won't work because it's not an Object, you are justing decrating a function.

Sunday, November 6, 2022
3

As you've rightly pointed out, support for the community mssql driver is not compiled into the latest Windows PHP binaries.

Presently the current stable release of MDB2 (2.4.1) does not support the official Microsoft sqlsrv native driver.

However if you're willing to live on the edge a bit with the beta release of MDB2 then all is not lost. There is a Microsoft native sqlsrv driver that is part of the 2.5.0b3 release:

http://pear.php.net/package/MDB2_Driver_sqlsrv

First of all ensure that you've installed the Microsoft native driver as I described in my answer here:

Connection between MSSQL and PHP 5.3.5 on IIS is not working

Then to install the PEAR MDB2 sqlsrv driver do the following:

  1. If you've already installed MDB2 then uninstall it:

    pear uninstall mdb2

    If you already have any drivers installed then you'll need to uninstall these first:

    pear uninstall mdb2#mysql

  2. Next tell PEAR that you want to allow non-stable beta packages:

    pear config-set preferred_state beta

  3. Install MDB2_Driver_sqlsrv

    pear install MDB2_Driver_sqlsrv

    This will install the latest MDB2 beta and the MS sqlsrv driver:

    downloading MDB2_Driver_sqlsrv-1.5.0b3.tgz ...
    Starting to download MDB2_Driver_sqlsrv-1.5.0b3.tgz (29,468 bytes)
    .........done: 29,468 bytes
    downloading MDB2-2.5.0b3.tgz ...
    Starting to download MDB2-2.5.0b3.tgz (130,865 bytes)
    ...done: 130,865 bytes
    install ok: channel://pear.php.net/MDB2_Driver_sqlsrv-1.5.0b3
    install ok: channel://pear.php.net/MDB2-2.5.0b3
    MDB2: Optional feature fbsql available (Frontbase SQL driver for MDB2)
    MDB2: Optional feature ibase available (Interbase/Firebird driver for MDB2)
    MDB2: Optional feature mssql available (MS SQL Server driver for MDB2)
    MDB2: Optional feature mysql available (MySQL driver for MDB2)
    MDB2: Optional feature mysqli available (MySQLi driver for MDB2)
    MDB2: Optional feature oci8 available (Oracle driver for MDB2)
    MDB2: Optional feature odbc available (ODBC driver for MDB2)
    MDB2: Optional feature pgsql available (PostgreSQL driver for MDB2)
    MDB2: Optional feature querysim available (Querysim driver for MDB2)
    MDB2: Optional feature sqlite available (SQLite2 driver for MDB2)
    MDB2: Optional feature sqlsrv available (MS SQL Server driver for MDB2)
    MDB2: To install optional features use "pear install pear/MDB2#featurename"
    
  4. It's probably advisable to knock PEAR back to only allowing stable packages again

    pear config-set preferred_state stable

I just tried this with the following test script and I was able to connect to my local MS SQL Server and retrieve some data:

<?php
require_once 'MDB2.php';

$dsn = array(
    'phptype'  => 'sqlsrv',
    'username' => 'test',
    'password' => 'testpass',
    'hostspec' => 'localhost',
    'database' => 'PEARMDBTEST',
);

$mdb2 =& MDB2::connect($dsn);

if(PEAR::isError($mdb2)) 
{
    die($mdb2->getMessage());
}

$res =& $mdb2->query('SELECT * FROM TestData');

while (($row = $res->fetchRow())) {
    echo $row['TestDataRow'] . "<br/>";
}

?>
Saturday, September 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :