Page 1 of 1

Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 04:19
by DKY
Trying desperately to get xampp to talk with mssql server to no avail. I've looked over a few posts here on this forum but am having no luck. I went to this link
http://www.php.net/manual/en/sqlsrv.installation.php
and downloaded the drivers to my ext directory I then added these two lines in my php.ini file
extension=php_pdo_sqlsrv_53_ts_vc9.dll
extension=php_sqlsrv_53_ts_vc9.dll

Finally after restarting I tried this simple connection
Code: Select all
<?php


/* CONNECT TO THE DATABASE */
$connectionInfo = array('UID'=>'', 'PWD'=>'', 'Database'=>'dia2');

/*USE DB CONNECTION ABOVE TO CONNECT TO THE SERVER */
$conn = sqlsrv_connect('brksvp306\BRKSQL01', $connectionInfo);

/* STORE SQL IN A VARIABLE */
   $sql = "SELECT stcname, shipto FROM DIA2.dbo.dealers ORDER BY stcname";

/* RUNNING THE SQL */
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
    if( ($errors = sqlsrv_errors() ) != null) {
        foreach( $errors as $error ) {
            echo "<BR>SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
            echo "code: ".$error[ 'code']."<br />";
            echo "message: ".$error[ 'message']."<br />";
        }
    }
}


?>

only to get this error
Warning: sqlsrv_query() expects parameter 1 to be resource, boolean given in F:\xampp\WebPages\PR\PartReviewResultsms.php on line 14

SQLSTATE: IMSSP
code: -14
message: An invalid parameter was passed to sqlsrv_query.


Running
XAMPP 1.7.7
Windows Server 2003 sp2
PHP Version 5.3.8

Re: Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 08:40
by Altrea
Use sqlsrv_errors().
Your connection could not be established.

Re: Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 12:46
by DKY
Is there something more to sqlsrv_errors()? I'm using it above and it gives me this error
Warning: sqlsrv_query() expects parameter 1 to be resource, boolean given in F:\xampp\WebPages\PR\PartReviewResultsms.php on line 14

SQLSTATE: IMSSP
code: -14
message: An invalid parameter was passed to sqlsrv_query.


Trying to figure out why a connection can't be established since the same log in information works on the server that has the sql server on it using IIS so it's not the name, password or database information... I need to get the connection on this other server though as the IIS is locked down...
Do you think I could have the wrong extensions? or maybe I'm referring to the wrong extensions in php.ini???
Thanks in advance!

Re: Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 12:48
by Altrea
DKY wrote:Is there something more to sqlsrv_errors()? I'm using it above and it gives me this error

Because you don't place it everywhere it is possible.
You can use it also to check the result if sqlsrv_connect()

best wishes,
Altrea

Re: Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 12:54
by DKY
How do I do that? I tried
Code: Select all
echo sqlsrv_connect('brksvp306\BRKSQL01', $connectionInfo);

but nothing echoed out...

Re: Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 12:56
by Altrea
Thats why i linked to the sqlsrv_errors() documentation.
It contains a full example (i quote just the relevant part)
Code: Select all
<?php
$serverName = "serverName/sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}

Re: Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 13:04
by DKY
okay, so that's really weird.
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'
Yet it works on the local machine... Wonder if there's some sort of setting on the SQL server to allow externals as anonymous?

Re: Can't get PHP to talk to SQL Server via SQLSRV...

PostPosted: 02. April 2013 13:16
by Altrea
If you don't specify Username and Password Microsoft SQL Server tries to connect with the logged in user via Kerberos. This truely works on the local database server but can fail from remote connections.
But that is not a XAMPP problem and i don't have much experiences with MSSQL Servers to be any help for you.