0

I'm building a web-service for an Android application that needs to connect to a SQL Server database. I'm trying to connect through PHP (WAMP) on my home computer to the SQL Server database.

However, I don't have experience SQL Server and don't know how to proceed. For SQL Server, I'm using default settings and Windows Authentication, so I'm not sure what to type into the connection string.

Below you have my connection:

$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; 
$conn->open($connStr); //Open the connection to the database

I haven't found a concrete example anywhere so far, so I need to know what the variables $myServer, $myUser etc. need to be in the case of Windows Authentication.

Alternatively, how can I switch to a User-Name and Password Authentication in SQL Server?

LE: Using Microsoft SQL Server 2008 and SQL Server Management Studio

5 Answers 5

4

PDO is the accepted way to connected to different databases in PHP. It also have a driver for MS-SQL

Here is an example (From PDO MSSQL)

$con = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry...I'm quite the noob when it comes to these. So...I'll ditch my current attempt and go with PDO. And for that I'll need to install another driver for PHP, just like I installed the Microsoft one, right? :D
First of all check for support by running phpinfo(); You should see something like "PDO support enabled" "PDO drivers dblib, mysql, odbc, pgsql, sqlite, sqlite2" Now Im not sure but you need either dblib or sqlsrv (one of them is for *nix and one for windows afaik), if its there you can just use it, if its not then you need to add the appropriate .dll to php.ini in you server (wamp), look for something like: extension=php_pdo_mssql.dll (should be commented I guess) I work with linux so can't help you with the exact location of the files and extensions under windows. Good Luck :)
Here is a thread from wamp's server site this might be a good starting point. Also feel free to move this discussion to chat I can try to help you there
2

Microsoft made a driver for PHP, but I think it's better to go with PDO.

You can use PDO_SQLSRV:

$dbh = new PDO("sqlsrv:Server=$hostdb;Database=$dbname", $usr, $psw);

Or use PDO_DBLIB (not available on Windows since PHP 5.3):

$dbh = new PDO("dblib:host=$hostdb;dbname=$dbname", $usr, $psw);       

Comments

0

In a related note, the same fate awaits the mysql* extensions if people will ever stop insisting on using them (and the terrible "tutorial" sites stop advocating them) instead of the vastly superior PDO extension. – rdlowrey Feb 12 '12 at 14:58

Comments

0
$dbh = new PDO("sqlsrv:Server=$hostdb;Database=$dbname", $usr, $psw);

Comments

-1

According to: http://www.php.net/manual/en/function.mssql-connect.php,

mssql_connect($servername, $username, $password)

is how you connect to a Microsoft SQL database.

3 Comments

This extension is not available anymore on Windows with PHP 5.3 or later. php.net/manual/en/intro.mssql.php
RTFM answers should be comments, not answers.
In a related note, the same fate awaits the mysql* extensions if people will ever stop insisting on using them (and the terrible "tutorial" sites stop advocating them) instead of the vastly superior PDO extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.