Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

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?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Bruce Gust

ASKER

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?
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Sounds reasonable - I know I've done 'bad-practice' myself to hit a deadline, and then gone back to refactor.
But my thought processes are sound, correct? I cannot use dblib to implement a stored procedure, I have to use sqlsrv, yes?
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial