Solved

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

Posted on 2008-10-21
9
1,934 Views
Last Modified: 2013-12-13
Can I use PHP PDO(PHP Data Objects) to connect to SQL Server 2008? I can't seem to connect.
0
Comment
Question by:penname
  • 5
  • 4
9 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22768728
Show us what you have tried?
0
 

Author Comment

by:penname
ID: 22768828
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
 

Author Comment

by:penname
ID: 22768956
I am running PHP 5.2.6
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22769011
Please set error_reporting(E_ALL) and show us what the errors say.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:penname
ID: 22769145
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
 

Author Comment

by:penname
ID: 22769790
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22769904
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
 

Accepted Solution

by:
penname earned 0 total points
ID: 22769973
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22770018
Congrats!  Good day's work. ~Ray
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now