PHP SQL Server driver not connecting - php

I'm using the Zend CE server with PHP 5.3.5 and Eclipse Helios with PDT to develop a database-driven PHP web application. I have my dev environment setup and enabled the SQL_Srv driver through the Zend control panel.
My problem is in my script when I call the sqlsrv_connect() function, it fails to connect to my SQL Server db and always returns false. I've verified I can connect to the server through a System DSN as well as Microsoft SQL Server Management Studio, so I know it's there and I can talk to it. These same settings also work on my coworker's dev environment (the same as mine), so I'm not sure what's missing. Has anyone else had similar issues with the PHP SQL Server driver? The relevant snippet of code:
$serverName = 'dbServer';
$connectionInfo = array ( "Encrypt" => 0, "LoginTimeout" => 10, "Database" => "Test_DB", "UID" => "ABC", "PWD" => "123" );
//This is always false. Does not connect
$test_Connect = sqlsrv_connect ( $serverName, $connectionInfo );
$GLOBALS['_connection'] = sqlsrv_connect ( $serverName, $connectionInfo );
if ( $GLOBALS['_connection'] == false ) {
print_r ( sqlsrv_errors(), true );
die ( "Failed to connect to SQL Server '$serverName'." );
}

Turns out PHP SQLSrv driver requires the Microsoft SQL Server 2008 Native Client to function. Make sure it's the '08' client and not '05', I already had the native client for SQL Server 2005 installed while experiencing this issue. It was only after I installed the client for SQL Server 2008 that it worked properly. Hope this helps others.

Related

Setup MS SQL Remote Connection & Connect From Linux Hosting With PHP

Oke soo as the title said,
I have SQL Server 2005 installed on my windows 7 at home, and I have a website which is using linux (Ubuntu 14.04, can be upgraded if necessary),and the questions is
How to setup remote connection for SQL Server
What kind of driver or addons that I need to use for PHP on linux? SQLSRV ? is the driver safe to use?
Example for the driver that used to connect to SQL Server
And I see that people use ip to connect remotely, how do I get the ip for connecting to my own pc?
To setup remote connections follow the instructions here: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-remote-access-server-configuration-option
Yes the driver is safe. Getting started instructions here: https://www.microsoft.com/en-us/sql-server/developer-get-started/php/windows/
Example code
<?php
$serverName = "localhost";
$connectionOptions = array(
"Database" => "SampleDB",
"Uid" => "sa",
"PWD" => "your_password"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn)
echo "Connected!"
?>
You can get ip from ipconfig

Access Azure SQL Database from LAMP localhost in PHP

I would like to connect to by Azure SQL database by using a PHP-Script. I execute the following script by using LAMP:
<?php
$serverName = "tcp:***.database.windows.net, 1433";
$connectionOptions = array("Database" => "TryMe",
"UID" => "***#serverID",
"PWD" => "***");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
?>
Of course I checked the firewall rules on Azure. I allowed my IP to access the server.
But I'm getting the error message:
GET http://localhost/Ionic/test.php in the web console.
What I'm doing wrong?
Thanks for your help!
The function sqlsrv_connect requires the Microsoft Drivers for PHP for SQL Server which is only enabled in Windows system. As you are working in LAMP environment, you can try to use ODBC extension and Microsoft's SQL Server ODBC Driver for Linux.

Why doesn't sqlsrv_connect work like before?

So I've re-installed my "webserver". It had some OS issues and I decided it was time ! I copied/backed-up the entire WAMP folder and restored it nicely after reinstall. All of that works, just not the site like it should. In essence, the sqlsrv_connect seems not to connect ! It doesn't give me any error output. $conn = sqlsrv_connect($serverName, $connectionInfo); will just come out "false"
My setup :
'''
Windows 7 Enterprise 64 bit SP1
Wamp Version 2.5 (32bit)
Apache Version 2.4.9
PHP Version 5.5.12
MySQL Version 5.6.17
Microsoft SQL Server 2005 (on an external server)
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
'''
the PHP runs just fine in another server. That server however uses IIS.
$serverName = "first.second.third.fourth.fifth";
$connectionInfo = array("Database"=>"Databasename", "UID"=>"Username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if (!$conn) {echo "no connection to database/server";}
$sql = "SELECT * FROM Tablename";
$results = sqlsrv_query( $conn, $sql ); if($results === false) {print_r( sqlsrv_errors(), true);}
What I've tried so far :
checked that the server actually can be accessed. Same code works
fine on the IIS server.
tried to restore backups from previous PHP files I had.
checked the PHP.INI file(s) and checked that the sqlsvr extensions
are activated.
tried the same site on another wamp webserver.
Disabled AV to test if it's not maybe the connection/firewall to the SQL server.
checked that admin priv's are on the essential wamp exe's
What am I missing ? I truly think it's a small setting somewhere - just can't figure out where or what ! Maybe it's a setting that allows this system to connect to the SQL server ? This setting is enabled on the IIS server by default... ? Maybe I need to pass additional authentication somehow ?
Firstly, I used this to get the error message :
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
Then the issue was it needed the ODBC Driver which I got from here:
https://www.microsoft.com/en-us/download/details.aspx?id=36434
Downloaded the 64 bit version and installed it and all was working again.

Connecting to MS SQL server from PHP

I am trying to connect to a SQL Server database from PHP but I'm having problems with the connection string. Here is a rundown of what I've tried and the effect it has had:
Running PHP 5.3.13, Apache 2.2.22, Windows 7, and SQL Server 2008 R2
I've installed SQL Server Native Client 11.0
I've dropped using MSSQL driver, using the SQLSRV driver instead.
Loaded these extensions in php.ini: extension=php_pdo_sqlsrv_53_ts.dll, extension=php_sqlsrv_53_ts.dll,
extension=php_pdo.dll.
phpinfo() shows these as active:
Registered PHP Streams php, file, glob, data, http, ftp, zip, compress.zlib, phar, sqlsrv
PDO drivers mysql, odbc, sqlite, sqlsrv
sqlsrv support enabled
Connection code is as follows:
$serverName = "[servername]";
$connectionInfo = array( "Database"=>"[databasename]");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
The string is set up to use Windows authentication, which sqlsrv_errors() reports:
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[networkname]\[machinename]'
I have also tried this using my network id/pwd, resulting in
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[username]'
I do know that the database is up and functional, as I can connect and run queries without problem from the SQL Server client, and from sqlcmd. The SQL Client and the command line work when using my network/machine credentials. The command line client fails when I try with username/password. This would indicate that any PHP connection should use the Windows authentication to connect.
Someone in another thread suggested that changing permissions in a registry entry would work, but this did not help my issue. PHP 5.3 not recognizing Native Client to connect to MS SQL
I also had some success creating a system DSN. The connection test reports success:
Microsoft SQL Server Native Client Version 11.00.2100
Running connectivity tests...
Attempting connection
Connection established
Verifying option settings
INFO: A network alias was found for the DSN server. 'Protocol: DBMSSOCN; Address: [servername]' was used to establish the connection.
Disconnecting from server
TESTS COMPLETED SUCCESSFULLY!
I've searched extensively within stackoverflow.com, and the interwebs in general with no luck. Obviously the db connection is alive using other methods, but what is going wrong in my connection via PHP?
I got this solved by creating a new login using sql server authentication. I hope this helps somebody with similar issues.
SQLSRV uses the absence of a UID (uid==NULL || strlen(uid) ==0) to indicate that it should try set the "trusted connection" flag in the connection string, i.e. connect using the credentials implied by the execution context.
You can't pass a domain login and password (e.g. UID='MYDOMAIN\user') explicitly.
I had a weird compiler version issue with Microsoft's driver for PHP, and solved my connection problem with PHP's ODBC driver:
//$pdo = new PDO("sqlsrv:Server=$hostname;Database=$dbname;", $username, $password); // works with proper driver for PHP.
$pdo = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname;", $username, $password); // works with proper driver for ODBC and PHP ODBC.

Connecting to SQL Server 2008 through PHP

I need to connect to a SQL Server 2008 through PHP (WAMP, latest version). I have the sqlsrv drivers installed and set up and they do show up in phpinfo().
I'm using the following lines to connect to my DB, using Windows Authentication:
$serverName = "(local)";
$connectionOptions = array("Database"=>"MyTestDatabase");
$conn = sqlsrv_connect( $serverName, $connectionOptions) or die("Error!");
And I'm getting the following error:
Array
(
[0] => Array
(
[0] => IMSSP
[SQLSTATE] => IMSSP
[1] => -49
[code] => -49
[2] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
[message] => This extension requires the Microsoft SQL Server 2011 Native Client. Access the following URL to download the Microsoft SQL Server 2011 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
)
[1] => Array
(
[0] => IM002
[SQLSTATE] => IM002
[1] => 0
[code] => 0
[2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
[message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
)
)
Any help would be great, but please be specific since I really don't know my way around WAMP except for a few basics.
The error is caused by a permission issue in the registry. There is a key where the path to the native client is stored and the identity you are using in the application pool is getting denied access.
Download Process Monitor and follow the instructions on this post:
http://www.iislogs.com/articles/processmonitorw3wp/ (This tutorial shows how to do it on IIS, with WAMP you will need to find the .exe process that runs on memory)
Find the registry key where the process w3wp.exe is being denied access. That in the case of IIS, not sure what's the name of the process in WAMP but the procedure is the same.
In my case:
HKLM\Software\ODBC\ODBCINST.INI\SQL Native Client 10.0
Run regedit and find the key
Right click and go to Permissions
Add Network Service to the list and give it Read permissions.
Recycle the app pool and it should work
sqlsrv_connect PHP manual
Try this approach to see what the error actually is:
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
} else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
Based on your setup, you may need to revert to the mssql_connect and corresponding functions. This works fine with mssql 2008 and you don't usually lose meaningful functionality.
Here's an example of setting up a connection to the database. Depending on your configuration you may have to add port information, etc.
$hostname = "server.domain.com";
$database = "DatabaseName";
$username = "LocalSQLUserName";
$password = "P#ssw0rd";
$Connection = mssql_connect($hostname, $username, $password);
If you are running on a XAMP environment, then it would stand to reason that you are running everything locally. If that's the case, your server would be localhost or 127.0.0.1 and you can define your own accounts in SQL. As I said, I don't use Windows accounts myself, but you can add an NT account to your "server" and then in SQL Server Management tool set that user to have access to the DB you are using. Then you have set the account and password and know what they are. If you have trouble with the user account, you can try computerName\userName.
If you are not the admin, you'll need to get the relevant information from them.

Categories