Solved

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

Posted on 2008-10-21
9
1,947 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 110

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 22769011
Please set error_reporting(E_ALL) and show us what the errors say.
0
 

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 110

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 110

Expert Comment

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this article I will describe the Detach & Attach 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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

752 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