odbc_connect sql server - php

I'm trying to install a PHP5 Web Server on Windows 2000 Server IIS.
It runs ok the php files but the query to my SQL database gives error without any kind of message.
This is my connection:
$conn=odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
In phpinfo the obdc is enabled.
This connection on my WapServer runs good without errors.
The database is on a local server for both machines.
How can I resolve this?
Thanks.

After some try and error I achieved an answer, instead of SQL Server Native Client 10.0 is just SQL Server.
Here is the correct connect code:
$conn=odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);

Related

PDO Error “Adaptive Server is unavailable” when connecting to MS SQL Database

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;");

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 MSSQL server 2008 with PHP

We are using MSSQL server 2008 on Win server 2008 R2 and have installed Apache (2.2.19) and PHP (5.2.17) separately. We have installd the MSSQL module for PHP and usingthe following connection string:
$myc = mssql_connect(Server, SiteDatabaseUsername, SiteDatabasePassword) or die('Can\'t connect to mssql Database Server: '.mssql_get_last_message($myc));
$db = mssql_select_db(SiteDatabaseName, $myc) or die('Can\'t find database: '.mssql_get_last_message($myc));
But it gives us this error:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: localhost\MSSQLSERVER in D:\Apache2.2\htdocs\adminarea\_core.php on line 89
Can't connect to mssql Database Server:
We have used the IP:PORT, localhost\MSSQLSERVER and COMPUTERNAME\MSSQLSERVER but don't seem to be getting anywhere, can anyone help please?
A few things come to mind:
1) Make sure you have the sqlsrv driver package from MS, and make sure you're loading it properly in php.ini (use phpinfo() to verify).
2) If this was a vanilla installation of SQL Server, its likely that only windows authentication is turned on. Open the SQL Enterprise Manager and make sure you have SQL Server Authentication enabled on your database.
3) Make sure that you have enabled network access to SQL Server. I'm not immediately certain whether the new MS drivers use named pipes to communicate with the server, or if they expect that the server will be accessed via the network.
These are the issues I routinely encounter when getting PHP speaking to a new MSSQL server.

Trouble connecting to SQL Server with PHP

I'm trying to connect to my local SQL Server 2008 R2 (have also tried it with 2005, same result exactly) with PHP. I'm using PHP 5.1 which still supports php_mssql and mssql_connect().
for some reason PHP just won't find my server, I can connect via ODBC flawlessly an that's fine, but I would like to connect to SQL Server directly.
I have connected PHP to SQL Server a million times on different servers, this one seems to be the only one giving me issue.
This is my little test code to try and get the connection working.
//define connection garbage
$db['hostname'] = "USER90C6\SQLEXPRESS";
$db['username'] = "user";
$db['password'] = "password";
$db['database'] = "kal_auth";
//connection string
$conn = mssql_connect($db['hostname'], $db['username'], $db['password']);
//does it work? :o
if($conn)
{
echo "works";
}
else
{
echo "fails";
}
The error this code produces:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: USER90C6\SQLEXPRESS in C:\xampp\htdocs\test.php on line 9
fails
Is there something seriously wrong with my setup? or am I missing something.
Did you enable TCP/IP on the server's configuration tool?
Is the firewall blocking any ports you might be using?
Are your MDAC (microsoft data access components) updated?
turn on mssql.secure_connection in php.ini
I think you don't miss anything.. Your connection string seems to be right (you receive a "Unable to connect" error..).
In my opinion, you problem can be a version incompatibility or a user privileges mistake. First of all: look at DLL driver you are using in PHP and check it's compatibility with you MSSQL version.
Maybe can be a good idea a fresh PHP install, with the latest stable release, if it is possible. Give a look at: http://www.php.net/manual/en/mssql.requirements.php
Good luck.

Connect to SQL Server 2008 with PDO

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.

Categories