Solved

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

Posted on 2008-10-21
9
1,945 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 109

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 109

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 109

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 109

Expert Comment

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

789 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