Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1958
  • Last Modified:

Can I use PHP PDO(PHP Data Objects) to connect to SQL Server 2008

Can I use PHP PDO(PHP Data Objects) to connect to SQL Server 2008? I can't seem to connect.
0
penname
Asked:
penname
  • 5
  • 4
1 Solution
 
Ray PaseurCommented:
Show us what you have tried?
0
 
pennameAuthor Commented:
I can connect to MySQL just fine using the following syntax:

$conn = new PDO("mysql:host=dev;dbname=testdb", "testuser", "testpassword");

But when I try to connect to either MS SQL Server 2000 or 2008 (do not have 2005) I get errors with either of these.
$conn = new PDO("mssql:host=dev;dbname=testdb", "testuser", "testpassword");
or
$conn = new PDO("odbc:host=dev;dbname=testdb", "testuser", "testpassword");

Thanks,
Mike
0
 
pennameAuthor Commented:
I am running PHP 5.2.6
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
Please set error_reporting(E_ALL) and show us what the errors say.
0
 
pennameAuthor Commented:
When I trap PDO exception I get the following error (Although I am using the same credentials I use to connect from within NuSphere IDE and I have no problems finding the server from that environment):

'PDOException' with message 'SQLSTATE[01002] Unable to connect: SQL Server is unavailable or does not exist. Specified SQL server not found. (severity 9)'
0
 
pennameAuthor Commented:
ok, thanks to the following link, I am able to get it connected to SQL Server 2000...so this is progress.  I still need to connect to 2008 though.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23670847.html

So the following works for 2000:

$conn = new PDO("odbc:Driver={SQL Server};Server=dev;Database=testdb;Uid=testuser;Pwd=testpassword", "testuser", "testpassword")
0
 
Ray PaseurCommented:
penname: You've seen the examples here, right?

http://us2.php.net/manual/en/ref.pdo-dblib.php

They seem to want to point us in the direction of this:

http://us2.php.net/manual/en/ref.pdo-odbc.php

Grmmphff. ~Ray
0
 
pennameAuthor Commented:
The syntax I mentioned above also works for 2008:

$conn = new PDO("odbc:Driver={SQL Server};Server=dev;Database=testdb;Uid=testuser;
Pwd=testpassword", "testuser", "testpassword")

or

$conn = new PDO("odbc:Driver={SQL Server Native Client 10.0};Server=dev;Database=testdb;Uid=testuser;
Pwd=testpassword", "testuser", "testpassword")

both work.  so I guess I have my answer.

thanks.
0
 
Ray PaseurCommented:
Congrats!  Good day's work. ~Ray
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now