Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Connecting to MSSQL examples

Hello, I was wondering if you had any examples of connecting to a MSSQL Database using PHP. I have connected to MySQL before no problem but haven't been able to find any examples connecting to MSSQL server database. Any help or tutorials or examples would be greatly appreciated.

Micahyah

6 Answers

Wow Godaddy will not add the MSSQL extensions for php and will have to create my site using ASP.NET instead. What crappy service from Godaddy.

Yeah they are terrible. My friend chose them for his hosting pln on a site I was building for him. After dealing with their terrible microsoft servers for an entire day. My friend changed to their linux servers at my request to give them one more chance. I still hate trying to find anything on that site besides a domain name.

Micahyah,

I have a Linux hosting plan with them as well, and I am not happy with it either. Would it be possible to use a MySQL database in your project instead of MSSQL? I've used MySQL with their Linux servers, and it is fairly easy to implement.

Maybe you could check out the mssql_connect function from the PHP documentation on php.net. Seems similar to MySQL. Here is a more detailed MSSQL example.

try
{
        $db = new PDO("mysql:host=yourHost;dbname=yourDBname;port=yourport#", "yourUsername", "yourPassword"); // connect
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); // Set
        $db->exec("SET NAMES 'utf8'"); 

    }catch(Exception $e)
{
        //If Connection cannot be made 
        echo "Could not Connect : Check connection Credentials";
        exit;
    }

For a great tutorial on how to successfully use PHP with MySQL I would recommend visiting the Using PHP with MySQL project here at treehouse.

http://teamtreehouse.com/library/using-php-with-mysql

Thanks, Aaron

Aaron, this is good but he was looking how to connect to MSSQL (Microsoft Server) not MySQL.

Oh my bad guys.

I am not familiar with connecting to MSSQL, but couldn't you just replace mysql with dblib or mssql? Or would this not return the expected results?

example

$db = new PDO("mssql:host=yourHost;dbname=yourDBname;port=yourport#", "yourUsername", "yourPassword"); 

or 

$db = new PDO("dblib:host=yourHost;dbname=yourDBname;port=yourport#", "yourUsername", "yourPassword"); 

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

From what I read on this PDO Object Forum Post it seems like that might work just fine. Nice find! Hopefully this solves Micahyah's problem.

Thank you all so much for your responses. Right now I just found out that my server with godaddy is not running the extensions for mssql server in php. I am going to call them and ask how to get them enabled. I will keep you all posted and I'm sure I'll have more questions for you all.