My environment:
- Windows Server 2008 R2.
- Apache Webserver.
- PHP 5.4.7
I've just downloaded and installed Microsoft Drivers 3.0 for PHP for SQL Server.
I've included "php_pdo_sqlsrv_54_ts.dll" into php.
Microsoft has included a helpful chm file with the drivers (SQLSRV_Help.chm).
In this help file there is a PDO example using windows authentication:
/* Connect using Windows Authentication. */
try {
$conn = new PDO("sqlsrv:server=$serverName ; Database=AdventureWorks", "", "");
$conn->setAttribute( PDO::ATTR ERRMOE, PDO::ERRMODE EXCEPTION );
}
But I have to connect to our MS SQL Server 2008 R2 using SQL Server Authentication!
What is the difference is between connecting to SQL Server
using PDO and Windows Authentication
using PDO and SQL Server Authentication
Is it just the connect string? Do I have to write:
$conn = new PDO("sqlsrv:server=$serverName; Database=AdventureWorks; UID=sa; PWD=abc");
or do I have to write:
$conn = new PDO("sqlsrv:server=$serverName; Database=AdventureWorks; User Id=sa; password=abc");
Thanks in advance
Update:
I found a resoure:
http://www.connectionstrings.com/sql-server-2008
There are optional second and third parameter in the constructor which specify the username and password as described here:
PDO::__construct
Creates a connection to a SQL Server database.
Syntax PDO::__construct($dsn [,$username [,$password
[,$driver_options ]]] )
Parameters
$dsn: A string that contains the prefix name (always sqlsrv), a colon,
and the Server keyword. For example "sqlsrv:server=(local)". You can
optionally specify other connection keywords. See Connection Options
for a description of the Server keyword and the other connection
keywords. The entire $dsn is in quotation marks, so each connection
keyword should not be individually quoted.
$username: Optional. A string that contains the user's name. To
connect using SQL Server Authentication, specify the login ID. To
connect using Windows Authentication, specify "".
$password: Optional. A string that contains the user's password. To
connect using SQL Server Authentication, specify the password. To
connect using Windows Authentication, specify "".
[...]
This implies that you can connect to SQL Server using PDO + SQL authentication like this:
$conn = new PDO(
"sqlsrv:server=$serverName ; Database=AdventureWorks",
"sa",
"strong password"
);
The connection options section also mentions that you cannot use UID and PWD parameters in connection strings when using PDO_SQLSRV.
Related
I'm trying to connect to a MSSQL database using PDO with odbc. I'm aware that there's a package SqlSrv (but for some reason that package (.dll) won't load properly). So I found some documentation arguing it's also possible with PDO. In my PHP.ini I've enabled the extension php_pdo_odbc.dll and it loads fine.
My connection string looks like this:
$conn = new PDO(
'odbc:
Driver=SQL Server;
Server=MyServer\MyInstance;
Database=MyDatabaseName;
Trusted Connection=Yes;',
'MyWindowsUserName',
'MyWindowsPassword'
);
I've tried various properties (for example by prepending the domain to the username, switching with the authentication options User Id, UID, Password, PWD and Trusted Connection) but I keep getting the message
SQLSTATE[28000] SQLDriverConnect: 18456 [Microsoft][ODBC SQL Server
Driver][SQL Server]Login failed for user 'MyWindowsUserName'.
Any suggestions on how to connect to the database with my Windows Account? (that's the only way for me to connect to the database)
Try removing the username & password
$conn = new PDO(
'odbc:
Driver=SQL Server;
Server=MyServer\MyInstance;
Database=MyDatabaseName;
Trusted Connection=Yes;'
);
I have authenticated by Windows with following PHP statement:
This is my code:
$ Conn = new PDO ("odbc: Driver = {SQL Server}; Server=JAMILI-PC\SQLEXPRESS; null; null");
I am using Windows 2008.
I hope it solves your problem.
TLDR: I'd like to connect a php application to a MS SQL server through an active directory account but haven't had much luck finding documentation on how to do that.
Most of the issues that people seem to have with this are pretty complicated I think I just don't have a strong enough grasp of the concept. When I try to connect it to the server, I get:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it.
The code I'm using is:
$user_name = "ADAccountName";
$pass_word = "ADAccountPW";
$datbase = "myDatabase";
$server = "#ipaddress#:#port#";
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
I created a user "sa" through MS SQL Server Management Studio and attached it to an AD account (I don't seem to have access to create one without picking an AD account).
I've had luck connecting it to a database using MySQL Workbench (for testing) and a dedicated username/password, but the actual database I need to use is run by MS SQL Server Manager with an AD account. I've spent the greater part of the day going through forums and Microsoft's documentation on SQL Server Manager but I haven't found anything that can help me out with this.
I'm using php v. 5.2
Depending which version of PHP you are using you could do the following
resource mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )
URL - http://php.net/manual/en/function.mssql-connect.php
This would give you direct connection to the DB.
If you're trying to connect to MSSQL you will need to do a few things to your web server first.
1.You will need to add PHP to your windows startup
2.Then you will need to install SQL Server Native client
3.Install the SQL Server drivers for your web server. SQLSRV30 is for PHP 5.4, SQLSRV31 is for PHP 5.5
4.Then go to your php.ini file and enable these extensions.
This will enable you to use PHP Data Objects.
You can connect to the DB like this:
try
{
$conn = new PDO( "sqlsrv:server=$serverName ; Database=AdventureWorks", "sa", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ) );
}
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.
I need the guidance help on ODBC connection string in PHP, which I am using to connect to MS SQL Server.
<?php
$server = 'UKEMO03';
$database = 'mtpFetch';//the database to connect to
$user = 'shoabg';// the user has PERMISSIONS AT THE DATABASE
$pass = 'Shsx12x';//and here the user's password
$dsn = "Driver={SQL Server};Server=$server;Database=$database;";
$connect = odbc_connect($dsn, $user, $pass);
?>
Above code works treat, but i have a requirement of not to specify the username and password and use the connection string with Integrated Security. I can't find anything on internet and I can't change the connection string method because I've completed most of my work and by adapting another connection method will require a massive change in a code in theory its not an option for me.
Is there a way we can create Integrated Security connection to connect MS SQL Server in PHP using ODBC
Please help i don't know what to do
This will connect you to the ODBC master database (cathalog).
I did not succeed to connect to another database. There is an option for default database in the configuration of ODBC SDN, but has no effect
("Chenge the default database to").
$this->dbHandle = odbc_connect("Driver={SQL Server};Server=localhost;Integrated Security=SSPI", '', '');
If you use PHP ini make sure you use:
mssql.secure_connection=On
On the database server, make sure you have Windows Authentification associated with your login username
I'm trying to connect using a string:
odbc:Driver={SQL Server Native Client 11.0};Server=(localdb)\v11.0;Database=test;uid=sa;password=123321;
Result: SQLSTATE[28000] SQLDriverConnect: 18456 [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'sa'.
When I try to connect using Windows ODBC Data Source Administrator the connection is successful.
What the problem might be in?
The network user 'sa' does not have permission to the Microsoft SQL Server.
The best way to provide network users access to Microsoft SQL Server is to create a Windows group (for example EGUSERS) and permit the Windows group Server Access at the Security Logins within Microsoft SQL Server.
Put all network users that need to have access to Microsoft SQL Server to the Windows group (EGUSERS).
Check if you have the right authentication mode set on the MSSQL Server:
https://msdn.microsoft.com/en-us/library/ms188670.aspx
Also you have two ways to connect to MSSQL through PHP/PDO by using the PHP_PDO_ODBC extension which uses the ODBC driver given in the connectionstring or use PHP_PDO_SQLSRV_xx_TS or PHP_PDO_SQLSRV_xx_NTS extension which you can find here (only for 32bit PHP!) https://www.microsoft.com/en-us/download/details.aspx?id=20098 or use the unofficial 64bit here http://robsphp.blogspot.nl/2012/06/unofficial-microsoft-sql-server-driver.html
connection string when using PHP_PDO_SQLSRV_xx_(N)TS extension:
$hostname='127.0.0.1';
$dbname='test';
$username='user';
$password='pw';
$dbDB = new PDO("sqlsrv:Server=$hostname;Database=$dbname", $username, $password);
connection string when using PHP_PDO_ODBC extension:
//use any of these or check exact MSSQL ODBC drivername in "ODBC Data Source Administrator"
$mssqldriver = '{SQL Server}';
$mssqldriver = '{SQL Server Native Client 11.0}';
$mssqldriver = '{ODBC Driver 11 for SQL Server}';
$hostname='127.0.0.1';
$dbname='test';
$username='user';
$password='pw';
$dbDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);
When testing a simple query which returned 66 records using PHP_PDO_ODBC extension took ~500ms (for all three MSSQL ODBC drivers) but when using the 64bit(!) PHP_PDO_SQLSRV_TS it took ~5000ms. 10 times slower! Have not yet tried 32bit or the NTS variant.
My dev PC is Windows 7 SP1 using WAMPx64 PHP 5.5.12 and I used PHP_PDO_SQLSRV_55_TS