Connecting to SQL Server 2012 Using PHP OOP

I've got my test data all set up, I've got a table of data ready to be revealed via my first PHP OOP adventure. Problem is, I can't decipher how to establish a connection between my page and the database using an OOP approach.

I've seen several web articles on it, but I get skittish when I see the SQL Server Management Studio 2008 version having just spent several hours uploading the 2012 version after running into problems.

So, how do I set up a database connection to my SQL database using an OOP PHP approach?
brucegustPHP DeveloperAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
I think you have no reason to parenthesis around the server name in...
$serverName = "(CORP-LOANER114\SQLEXPRESS)";

Open in new window

0
 
Ray PaseurCommented:
You can use PDO.  This article is about MySQL, not SQL Server, but the principles are all the same.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
Dave BaldwinFixer of ProblemsCommented:
You pretty much have to use the Microsoft SQL drivers for PHP.  They have one version for Windows and another that works on RHEL.  http://www.microsoft.com/en-us/download/details.aspx?id=20098  They include the PDO drivers.
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
brucegustPHP DeveloperAuthor Commented:
Yo, Dave!

I've got the drivers and I've installed them in the appropriate directory (C/Program Files/iis express/PHP/v5.3/ext) but I'm stuck now as far as where I go from here.

I've looked at some tutorials and it would appear as though I have to edit my ini file in order to activate the drivers, but I can't tell and then I'm still struggling to figure out the actual syntax that facilitates the actual connecting to the database.

Thoughts?
0
 
brucegustPHP DeveloperAuthor Commented:
Progress, I think...

I added the necessary entry into my php.ini file like so...

; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.
;

extension=php_sqlsrv_53_ts.dll

I found a thread you had contributed to that referenced the importance of the Thread Safe version setting (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28102807.html) and I confirmed from my php.ini file that the Thread Saftey feature has been disabled, hence the "ts" as opposed to the "nts."

So, what's next?
0
 
Dave BaldwinFixer of ProblemsCommented:
Since you are using IIS-Express, I would guess that you do not have a "Pro" version of Windows.  I think that since you are running IIS, you probably need the NTS version of the driver.  

The code that is required is in the CHM help file for the driver.  It is also on the PHP man pages:  http://us2.php.net/manual/en/book.sqlsrv.php
0
 
brucegustPHP DeveloperAuthor Commented:
OK, Dave, I think we're poised on the threshold of greatness, but not quite there yet.

First off, I've confirmed by looking at my phpMyInfo page that the sqlsrvr support has been enabled so the correct files have been referenced in the php.ini file, so I think we're good to go there.

I'm using this code to connect:

<?php
/*
Connect to the local server using Windows Authentication and specify
the AdventureWorks database as the database in use. To connect using
SQL Server Authentication, set values for the "UID" and "PWD"
 attributes in the $connectionInfo parameter. For example:
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"AdventureWorks");
*/
$serverName = "(CORP-LOANER114\SQLEXPRESS)";
$connectionInfo = array( "Database"=>"symbion_cer");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn )
{
     echo "Connection established.\n";
}
else
{
     echo "Connection could not be established.\n";
     die( print_r( sqlsrv_errors(), true));
}

//-----------------------------------------------
// Perform operations with connection.
//-----------------------------------------------

/* Close the connection. */
sqlsrv_close( $conn);
?>

Open in new window


...and here's the error I'm getting:

Connection could not be established. Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. [message] => [Microsoft][SQL Server Native Client 11.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) ) 

Open in new window


I'm looking at my SQL Server Management Studio and I've got these settings:

Authentication Method: Windows Authentication
User Name: SYMBION/bgust
Database: symbion_cer
Computer Name: CORP-LOANER114

What do you think?
0
 
brucegustPHP DeveloperAuthor Commented:
Dave, I got it!

I used an ODBC approach. Went into my Administrative Tools, set up a connection and BAM!

It's healthy too in that the project I'm working on is using that dynamic so it's good practice.

Thanks for weighing in!
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.