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.
Related
I'm trying to connect to a MSSQL database with odbc in php.
My issue is similar to this issue, I can connect with tsql, but not with php.
The answer to the problem not work because I think I don't have SELinux installed (I don't know what it is, but pacman not found this package (or with a similar name) on my computer)
I don't understand why it doesn't work, odbc is installed and detected by php
print_r(PDO::getAvailableDrivers());
Array ( [0] => odbc )
I'm connecting by doing that:
$dsn = 'odbc:Driver=FreeTDS;Server=127.0.0.1;Port:1433;Database=[my base name]';
$pdo = new PDO($dsn, $user, $password);
My base is not in local, I use a ssh tunel because the base in accessible only at my school, and we need an ssh tunnel. And it work, I can connect myself to the base with tsql.
When I was connecting PHP to our MS SQL server, I could never get a connection established using ODBC drivers.
Instead, I downloaded the official PHP > SQL SERVER drivers direct from Microsoft. You can find them here.
Once installed, you must configure your php.ini file to include the new drivers, restart your web server and then use the following to open a new connection:
$conn = new PDO("sqlsrv:Server=SERVER_IP,1433;Database=DATABASE_TO_OPEN;");
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.
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
When I run apache (via xampp) as a standalone server not as a service (on Windows Server 2008)
with the following connection code everything works fine (username and password removed )
$server = "WMS";
$link = odbc_connect($server,'','');
if (!$link) {
die('Something went horribly wrong while connecting to MSSQL');
}else {echo('');}
If however I change apache to run as a service in Windows the connection breaks and I get the following error message
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in
C:\xampp\htdocs\Dev\well.php on line 30
Something went wrong while connecting to MSSQL
Please read documentation: http://uk.php.net/manual/en/function.odbc-connect.php
$server = "WMS"; suggests that you have ODBC alias/data source configured with that name. Error message clearly says that data source with such name (WMS) is not found. On Windows 7/Vista/XP/Server you can configure them at "Start -> Administrative Tools -> Data Sources (ODBC)" -- path can be different on older OS. In any case -- look for "Microsoft ODBC Data Source Administrator".
Instead of using alias, I would recommend (the way I always connect) to use full DSN name, e.g.
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$link = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
In this case everything is part of the script and no external dependencies.
BTW -- instead of using ODBC Functions, I would recommend using PDO & driver specially for MS SQL Server: PDO_SQLSRV -- http://uk.php.net/manual/en/ref.pdo-sqlsrv.php (or Microsoft SQL Server Driver for PHP if you prefer old procedural style -- http://uk.php.net/manual/en/book.sqlsrv.php )
I am trying to connect to SQL Server 2008 (not express) with PHP 5.2.9-2 on Windows XP sp2. I can connect to the remote SQL Server fine from SQL Server Management Studio from the same machine.
My first attempt was this:
$conn = new PDO("mssql:host={$host};dbname={$db}", $user, $pass);
Which gives this error:
PHP Fatal error:
Uncaught exception 'PDOException' with message
'SQLSTATE[0100] Unable to connect: SQL Server is unavailable or does not exist.
Access denied. (severity 9)'
Second attempt (found on experts-exchange)
$conn = new PDO("odbc:Driver={SQL Server Native Client 10.0};Server={$host};Database={$db};Uid={$user};Pwd={$pass}",
$user, $pass);
This works, but I can't use PDO::lastInsertId(), which I would like to be able to:
Driver does not support this function: driver does not support lastInsertId()
Do you have any suggestions? Any help would be greatly appreciated.
i'm pretty sure that pdo with the mssql data server type requires dblib to connect. do you have dblib installed on the machine? make sure you can find ntwdblib.dll somewhere in the path on your system. i know this doesn't jive with the error message you're getting, but i'm not sure i trust the error message.
I updated ntwdblib.dll as suggested here and it now works.
Unfortunately I still can't use PDO::lastInsertId() because apparently this driver does not support it either, so it wasn't really worth the hassle. I can however use the equivalent SELECT ##IDENTITY as Id.
That you cannot use SQL Server authentication because only Windows authentication is permitted.
Check if the server is running Mixed mode authentication.
Also check if this SO question helps you.