Why am I getting this error?

I'm using the following code to connect to MSSQL server via PHP PDO:

include('mssql_db_cred.php');
$mssql_pdo = new PDO("sqlsrv:Server=".$mssql_cred_data['server'].";Database=".$mssql_cred_data['dbname'],$mssql_cred_data['user'],$mssql_cred_data['pw']);
$mssql_pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
if(!$mssql_pdo)
{
      echo "no connection";
}

The error I get back is:

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /var/www/html/testsite/c0gusb1/sql_srv.php:7 Stack trace: #0 /var/www/html/testsite/c0gusb1/sql_srv.php(7): PDO->__construct('sqlsrv:Server=g...', 'xxxxx', 'xxxxx!') #1 {main} thrown in /var/www/html/testsite/c0gusb1/sql_srv.php on line 7

I'm running PHP version 5.5.21 and I'm operating under the impression that the sqlsrv driver is part of that version. No idea how to proceed or what to look for.

Is there something that has to be installed? What am I lacking?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
They're not available by default - you'll need to install the drivers yourself (or ask your hosting provider to do it)

https://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
Bruce GustPHP DeveloperAuthor Commented:
Thanks, Chris! I didn't see that.

Quick question. I've got another person on my team who is using dblib rather than the sqlsrv driver. My installing the new driver won't compromise his code, will it?
Chris StanyonWebDevCommented:
No - won't effect other's code - PHP will have several DB drivers installed, and it's your own code that chooses which driver to use,

Out of interest, if he's using the dblib driver to connect to MSSQL, any reason why you can't use that as well ?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Bruce GustPHP DeveloperAuthor Commented:
Yes, and I'm glad your brought that up.

I want to use some stored procedures that are already in place and I've been told that if I'm running PHP 5.3 or later, I'm not going to be able to use the dblib libarary to do that. Rather, I've got to use sqlsrv (http://php.net/manual/en/ref.pdo-dblib.php). That's my impetus for doing this.

Here's the background of this past week / project you can read if you want to get the whole picture in your head.

I'm part of a team that's part .NET and PHP. I've been tasked with re-creating a Project Report document that's based totally on MSSQL and .NET. I got it to work using the dblib library, but I wound up doing some things in an effort to honor a deadline that fall short of best practices. Namely, I did a select statement at the top of the page where I asked for some distinct id's, then did another query later in the page where I looped through those id's and retrieved some more detailed information.

I ran into a problem, however, because apparently T-SQL has a low tolerance for querying the same data in the context of the same connnection. In other words, I should've have incorporated some left joins at the top of the page and do all my heavy lifting in the context of one query rather than breaking it up into what amounted to looping through same data twice. This was brought to the surface by the fact that my code would loop through the first few rows then quit. No apparently problem, but a defect nevertheless. The only way I got around it was to do my second query in the context of a new connection to the database.

When I showed this to my counterpart, she was very gracious but very emphatic in saying that what I was doing would work fine for a few hundred rows, but would be unacceptable if we were querying millions of rows and I get that.

So, while I have something I can bring to the table as far as something that is functional, I want to go back and "polish" what I've done and incorporate those stored procedures that she wrote and do this right.

What do you think?
Chris StanyonWebDevCommented:
Sounds reasonable - I know I've done 'bad-practice' myself to hit a deadline, and then gone back to refactor.
Bruce GustPHP DeveloperAuthor Commented:
But my thought processes are sound, correct? I cannot use dblib to implement a stored procedure, I have to use sqlsrv, yes?
Chris StanyonWebDevCommented:
Not entirely sure - I only ever work with mySQL in PHP - The only time I work with MSSQL is in C#.

Having just done a quick google, there's nothing to suggest that you can't use dblib to work with Stored Procedures.
Dave BaldwinFixer of ProblemsCommented:
No, you can't use 'dblib' or sqlsrv' in Linux, only in Windows.  "/var/www" in the error message tells me you are running on Linux now.  Those drivers use Windows programs to run them.  There are NO equivalents in Linux.  The 'mssql' driver is still available in Linux... but it's not actually the same driver that was formerly available in Windows.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.