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

asked on

I think I know what this error means, but I need some other eyes on it...

Using PDO, connecting to a SQL Server. I get this error:

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2001] The connection string may not be right. Please visit portal for references

I've been able to confirm that I am passing values into my method that initiates the database connection. I'm thinking that "the connection string may not be right" means that the connection is failing because I'm not giving it the right sign in parameters.

I want to sound intelligent before I go and state that the values are wrong when it may, in fact, be something else.

What do you think?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Can you connect with any other extension?

Maybe show us the connection credentials and the method you're using.
Avatar of Bruce Gust

ASKER

Ray, I've been at this a couple of hours now and I've got some bottom lines, but I'm still coming up empty.

The first error was resolved when I realized I was calling the wrong method. I was trying to connect to my MySQL database with the MSSQL credentials, hence the mess.

Once I got that squared away, the next problem was getting the necessary drivers to assist with this:

private static function CreateDatabaseConnection($sqlServerName, $sqlDatabaseName, $sqlUserName, $sqlUserPassword)	{
		$databaseConnection = new PDO("sqlsrv:Server=$sqlServerName;Database=$sqlDatabaseName", $sqlUserName, $sqlUserPassword);	 
		return $databaseConnection;
	}
	

Open in new window


I found the driver (php_pdo_sqlsrv_56_nts.dll and php_pdo_sqlsrv_56_ts.dll), installed them in the correct directory as per the phpinfo() display (c:/wamp64/bin/php/php5.6.25/ext/) and made sure the necessary additions were made to the correct loaded configuration file (C:\wamp64\bin\apache\apache2.4.23\bin\php.ini ):

extension=php_pdo_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_nts.dll

When I look at my extension using the WAMP server display, I see that they've been installed:

User generated image
But...

When you go out to the phpinfo page, you see an obvious disconnect in that the PDO files aren't reference:

User generated image
The fact that the extension display shows that there are "no extensions" for the sqlsrv drivers isn't lost on me, but I can't figure out what else I can do other than place them in the correct extension directory.

What am I missing?
Not sure if the port makes any difference, but I'm viewing all of my WAMP pages through localhost:8079.
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa 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
Julian!

I eliminated the superfluous nts version. As far as the 64bit version, I wasn't able to find anything other than a 32bit version. I found this while I was looking: http://stackoverflow.com/questions/23479544/is-there-mssql-server-driver-for-php-on-a-64-bit-environment ...and that had me convinced that I was working in the wrong directory (I had that problem once before with a different install).

But if there is a 64bit version of this driver, that might explain why nothing's working.

I'm going to do a restart and see what happens....
Julian, I came across this: https://social.technet.microsoft.com/Forums/en-US/9a2bc7af-3cea-47b5-b5e5-3de8b1ac46a4/compile-64bit-sql-server-driver-for-php-phpsqlsrvdll?forum=sqldriverforphp

Seems like there isn't a 64bit version of this driver...

If that's the case, what are my options?
OK, I've punted sqlsrvr and instead gone with ODBC (PDO_ODBC). Was able to confirm that it's up and running via the phpinfo() file, but I don't know if I'm calling it correctly. Here's my code:

$databaseConnection = new PDO("ODBC:Server=$sqlServerName;Database=$sqlDatabaseName", $sqlUserName, $sqlUserPassword);       

The error I get back is:  Uncaught exception 'PDOException' with message 'could not find driver' in C:\wamp64\www\sandbox\common\DatabaseFactory.php

What am I missing?
What you're trying is probably not going to happen.  'sqlsrv' IS the ODBC driver for Microsoft SQL Server.  And I believe it is only available in 32-bit.
So, how do I proceed? If my system is a 64bit, how do I go about connecting to a MSSQL database?
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
Seems like ODBC could work based on what it says in the PHP Manual: http://php.net/manual/en/ref.pdo-dblib.php. There's got to be a way to pull this off...
If my system is a 64bit, how do I go about connecting to a MSSQL database?
With PHP, I don't know that you can.
Dave, help me understand this...

In the past, I've used this:

$mssql_pdo = new PDO("dblib:host=".$mssql_cred_data['server'].";dbname=".$mssql_cred_data['dbname'],$mssql_cred_data['user'],$mssql_cred_data['pw']);

That was a few years ago. According to http://php.net/manual/en/ref.pdo-dblib.php, that extension isn't available anymore. The recommendation is to go with the SQLSRV, but that's not an option apparently because it doesn't come in a 64bit configuration.

Your comments make it sound like it's not a question of just activating the right extension and installing the appropriate dll file as has been the situation in the past. Instead it sounds like you have to have a driver also. This is new territory for me and I've been banging my head up against the wall all day over this.

So, where am I at? How do I get this thing to run?
I know that 'sqlsrv' works fine in 32-bit PHP but it does require the SQL Native Client or the MS ODBC driver depending on the version of SQL Server you have.  Where are you planning on running this code when it 'works'?
OK. Where do I get the driver and where do I put it once I download it?
One other thing - and I can't help but think that this is an unhealthy anomaly - but I can't find anything online about it.

Take a look at what I've got in this screenshot and look at the bottom where it says, "No extension."

User generated image
I've got everything installed...oh, but wait...

Nothing shows up in phpinfo and when I look at my php error log:

[22-Feb-2017 21:50:49 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp64/bin/php/php5.6.25/ext/php_pdo_sqlsrv_56_nts.dll' - %1 is not a valid Win32 application.

 in Unknown on line 0

[22-Feb-2017 21:50:49 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp64/bin/php/php5.6.25/ext/php_sqlsrv_56_nts.dll' - %1 is not a valid Win32 application.

 in Unknown on line 0

[22-Feb-2017 21:50:49 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp64/bin/php/php5.6.25/ext/php_pdo_sqlsrv_56_nts.dll' - %1 is not a valid Win32 application.

 in Unknown on line 0

[22-Feb-2017 21:50:49 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp64/bin/php/php5.6.25/ext/php_sqlsrv_56_nts.dll' - %1 is not a valid Win32 application.

What does that mean?
is not a valid Win32 application.

Mixing 32 and 64bit - will generate this error - they have to be the same arch otherwise no dice.

Might be an idea to download 32 bit PHP and install side by side with WAMP and then see if that gives you the results you need - you can test from the command line against a test script that attempts the connection.
'php_sqlsrv_56_nts.dll' is a 32-bit and 'nts' is for use with IIS, not Apache.  Apache requires 'php_sqlsrv_56_ts.dll'.  In addition even with everything else right, you probably want to get rid of all the 'sqlsrv' extensions that are not being used.

32-bit PHP is not going to work with 64-bit WAMP because of the way that WAMP is structured.  You would need a complete 32-bit installation.

Here https://www.microsoft.com/en-us/download/details.aspx?id=50420 is the MS ODBC 13 driver download.
This might be too simplistic, but why not just use MySQL?
@Dave
32-bit PHP is not going to work with 64-bit WAMP because of the way that WAMP is structured.  You would need a complete 32-bit installation.

That was not the suggestion - PHP can be installed stand alone without having to be integrated to anything else - I have exactly this setup on my machine. Install to its own folder then open command window - browse to folder and run php against script on the command line. Works perfectly. The idea was to do a 32 bit test against a simple script to see if a different result was obtained.
I suppose.  All of the same things need to be installed and set up.  The 'sqlsrv' driver needs to be copied to the 'ext' directory in the new installation and the MS ODBC driver or the SQL Native client still needs to be installed to support it.
Gentlemen! Thanks so much for your time and input! We're getting closer!

Ray: I've got to get this configuration in place because the client is using a MSSQL database and that's a fixture I can't alter.

Julian: I installed a 32bit version of WAMP, but not all of the services function out of the box. That's understandable, I imagine, because you did say that I needed to test its functionality using Command Line. I'm still a pig on roller skates when it comes to the Command Line interface so I got as far as seeing a yellow icon and I remembered how that was my initial cue to back up and install the 64bit version. And that does work, so at least the WAMP platform is in place.

Dave: I'm working in a shop that's primarily .NET so that explains why I was directed to install the "non-thread-safe" versions of the .dll files. And just to explain back to you what you told me, the nts files are 32 bit and to be used with IIS, hence the disconnect when you're attempting to use them in an Apace configuration.

All that said, here's where I'm at / what I've done:

Cleaned up all of the superfluous .dll files so my extension directory now looks like this:

User generated image
My cleaned up ini file looks like this:

User generated image
When you look at the active extensions via the side menu, it looks like this:

User generated image
The phpinfo file, however, still shows a disconnect:

User generated image
...but I'm assuming that's because of the system not being able to find the driver, which, coincidentally, is the error I get when I go out to the page:

User generated image
But here's the thing:

When I downloaded the driver from  https://www.microsoft.com/en-us/download/details.aspx?id=50420, this is what I got:

User generated image
I'm getting the same dll files that I've already added.

What's missing?
Julian: I installed a 32bit version of WAMP, but not all of the services function out of the box
Sorry - was not clear - I was referring to installing PHP only - not the full WAMP package. You can download PHP and extract it to a folder, configure the INI and then use it from the command line.
Did you install the WAMP 32-bit in the 'wamp64' directory?  Have you downloaded and installed the MS ODBC driver I mentioned above?  'sqlsrv' does not work without it and will not show up in 'phpinfo' without it.
Hey, Dave!

A couple of things: First off, my supervisor who's a .NET guy installed PHP in my IIS directory so I'm running everything through that dynamic and everything works. I say "everything works," but I still want to get it working within WAMP. That said, I'll download the driver you referenced earlier and see what that does. As far as installing the 32 bit WAMP in the WAMP 64 directory, I'm assuming you were being facetious, right? Or is that what I need to do?

I appreciate your time!
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
OK. I removed the 32 bit install altogether because when I try to run it on my box, I get an incomplete collection of functionality. When you start up WAMP, not all the services are running and it translates to yet another layer of problems on top of the dilemma I'm trying to solve. So, that ship has sailed.

I'm running WAMP on port 8079 and IIS is 8080.

The SQL drivers that are installed on the machine are working as far as the IIS dynamic that my supervisor installed. While I can get some work done in that environment, I still want to get the WAMP configuration happening in that I get more detailed error messages there than I do with IIS. Plus, I've invested some time into this and I want to see it through.

To recap: The extensions you install, as far as php_pdo_sqlsrv.dll etc represent one of two steps that have to be taken in order for PHP to talk to a SQL database. You also have to install the PHP SQL Drivers which are available through Microsoft. Here is where I went to get those drivers:

https://www.microsoft.com/en-us/download/details.aspx?id=20098.

If you go there and run the downloaded executable file, you get the php_pdo_sqlsrv.dll files. I already have those, so that piece is in place.

Those are the PHP SQL Drivers, but you still need the ODBC drivers, correct? Do I have the terminology correct?

So, that file is this guy, here: msodbcsql.msi. I'm getting that from here: https://www.microsoft.com/en-us/download/details.aspx?id=50420

When I go to run that file, I get this message:

User generated image
So, that's frustrating as well as disconcerting. If I have a "newer" file, why doesn't that do the job? If I do decide to uninstall it, does that affect the IIS dynamic which is currently working? How do I uninstall it?

Thanks for spoon feeding me on this, Dave...
'sqlsrv' is the PHP SQL Drivers.  'ts' versions for Apache, 'nts' versions for IIS.  If the code is running in IIS then the SQL Native Client (or MS ODBC driver) is doing it's job.  If you uninstall it then the code in IIS will stop working.  If everything is installed correctly, you will still have to restart Apache because that is the only time that it reads the 'php.ini' file that controls how PHP operates.
Dave, the only thing I have yet to do is restart my machine and I'm going to do that here in a little bit.

While I thought I had a connection to the database, I don't. Even on IIS. I was able to get some more verbose error reporting in place by adjusting the php.ini file and once I did that, I got this:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name 'tblparticipantreading'.' in C:\wamp64\www\sandbox\server\deletecustomtracker.php:14 Stack trace: #0 C:\wamp64\www\sandbox\server\deletecustomtracker.php(14): PDOStatement->execute() #1 {main} thrown in C:\wamp64\www\sandbox\server\deletecustomtracker.php on line 14

So, the good news is that I know I have both extension=php_sqlsrv_56_nts.dll and extension=php_pdo_sqlsrv_56_nts.dll installed. But here's the "driver" you keep mentioning. I've never been able to find that.

When I go here: https://www.microsoft.com/en-us/download/confirmation.aspx?id=20098, I get all of the php_sqlsrv dll files. I don't think I get the SQL Native Client driver. Is it something different and where do I get that?

Thanks!
On this page: https://www.iis.net/learn/application-frameworks/install-and-configure-php-on-iis/install-the-sql-server-driver-for-php, you get these instructions:

User generated image
Everything is intuitive except for step number 5. When you open up that file, you get this:

User generated image
...but I couldn't find any link for the Native Driver.

What am I missing?
If you look in your ODBC manager, you will see whether the SQL Native Client or ODBC Driver are installed.  They are system files, not PHP files.

I gave you the link for Microsoft® ODBC Driver 13 for SQL Server® above which is the current replacement for the SQL Native client.  All of the links for the SQL Native Client are part of the Feature packs for the different versions of SQL Server.  There are no direct links to SQL Native Client by itself.

https://www.microsoft.com/en-us/download/details.aspx?id=50420
I wound up using IIS since this is a .NET shop and the database connection worked just fine. Not sure why it worked and the WAMP configuration fell short. The gentleman that set me up used IIS Installer so it looked as though a lot of what I was trying to configure by hand was getting done automatically.

We'll go with that for now and if this issue surfaces again, I've got a lot of feedback to refer to.

Thanks!