How to use php with sql server?

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 11556 Views - Last Post: 26 March 2015 - 11:28 AM

#1 JohnDude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 15

How to use php with sql server?

Posted 20 March 2015 - 11:55 AM

Hello

I am trying to use PHP with SQL Server 2005. Currently I have wamp installed locally for testing but eventually it will have its own windows 7 machine, 64 bit which is the same machine I am using for testing. I have php 5.5.12 installed. I get this error "Call to undefined function sqlsrv_connect()"

I tried searching this question on the web and find tons of information most of it only partially specific to my situation and sometimes the information is conflicting.

Programming is not new to me but web programming is. I have suceeded in writing basic php code and I put it in my www folder under my wamp folder and can open php files in my browser and the code runs fine. I downloaded SQLSRV32.EXE from microsoft and gave it this folder "b:/wamp/bin/php/php5.5.12/ext" which it installed a slew of files into. Then I edited "B:\wamp\bin\apache\apache2.4.9\bin\php.ini" and added this line 'extension="b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_55_ts.dll".' It didn't work so I added all the files:
extension="b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_54_ts.dll"
extension="b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_54_nts.dll"
extension="b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_55_ts.dll"
extension="b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_55_nts.dll"
extension="b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_56_ts.dll"
extension="b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_56_nts.dll"

This didn't work either. phpinfo does not have a sqlsrv section. Can someone provide me a clue as to what this needs to make it work? A link, a tutorial, something. I can't find any clear instructions. But I do see a lot of people have problems getting this to work. I would even be happy with search terms that may provide something useful because "using sql server with php "undefined function mssql_connect"" doesn't work.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to use php with sql server?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,520
  • Joined: 12-June 08

Re: How to use php with sql server?

Posted 20 March 2015 - 11:59 AM

Wait, what? What did you install, and where? MSSQL would install as a normal app (in the programs folder.. and be running a continual database service.

You have wamp - why not use MYSQL?
Was This Post Helpful? 0
  • +
  • -

#3 JohnDude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 15

Re: How to use php with sql server?

Posted 20 March 2015 - 12:04 PM

I need to use MSSQL since the entirety of the companies data lives there.

Let me give a little background. We have an ERP application we use for a multitude of things, all bound to windows. The next app I would like to break this connection to allow growth in an OS other than windows. That is why I would like to use php to serve up the data to a variety of clients. But I can not get php to even load a sql server driver.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,520
  • Joined: 12-June 08

Re: How to use php with sql server?

Posted 20 March 2015 - 12:06 PM

Okay.. so you'll need a MSSQL instance running on the network.. and connect via the PDO instructions here:

http://php.net/manua...f.pdo-dblib.php
Was This Post Helpful? 0
  • +
  • -

#5 JohnDude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 15

Re: How to use php with sql server?

Posted 20 March 2015 - 12:07 PM

I tried to change the lines to this:
extension_dir = "b:/wamp/bin/php/php5.5.12/ext/"
extension=php_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_nts.dll

But that doesn't work either.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,520
  • Joined: 12-June 08

Re: How to use php with sql server?

Posted 20 March 2015 - 12:07 PM

.. or you know use PHP in an IIS environment.

http://php.iis.net/
Was This Post Helpful? 0
  • +
  • -

#7 JohnDude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 15

Re: How to use php with sql server?

Posted 20 March 2015 - 12:17 PM

I have downloaded all those drivers. How do I get php to load them?

I already have php running in wamp. I do not wish to wrestle with IIs. Besides, if php will not load the sql server drivers under wamp I have no reason to believe it will laod them under IIs. I can go through all the trouble of setting up another server just to be stuck with the same issue.

The problem I am having is php will not load any drivers. I can not find ant instructions on how to accomplish this.

phpinfo does not display any information relating to sqlsrv.

Thanks for that link, but the link explains how to do things AFTER the driver is loaded and running. It is a great resource for step 2. But I need to get past step 1 before I can even think about step 2.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6582
  • View blog
  • Posts: 30,736
  • Joined: 10-May 07

Re: How to use php with sql server?

Posted 20 March 2015 - 12:29 PM

As this question is about servers & services, & not php coding, moved the correct forum.
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1869
  • View blog
  • Posts: 5,831
  • Joined: 15-January 14

Re: How to use php with sql server?

Posted 20 March 2015 - 12:33 PM

The only thing you need to do is put the correct drivers into the extension directory, then edit php.ini to use them. You need to restart the web server after saving php.ini.

Do not install SQL Server into PHP's extension directory. That is not a good idea. Install it like you normally would if you were not trying to use it with PHP.

Don't use more than one driver. The drivers you listed are labeled as either thread-safe (ts) or non-thread-safe (nts). Do not use both of them. I believe for WAMP you'll use the thread-safe ones. Other than that, find the correct version number for your version of PHP and use that driver. You should only be loading a single driver.

If you don't see anything in phpinfo, then you need to do some debugging. Go through the php.ini file and check the various settings (you can search the documentation on php.net if you want to see what any option does). Find the setting for startup errors and make sure that is enabled. After you're done restart the web server again and try to load a page (phpinfo is fine). Go through the WAMP control panel to open the PHP error log and look there for the error messages when PHP starts up, it may report a problem loading the extension. You can also check Apache's error logs.
Was This Post Helpful? 0
  • +
  • -

#10 JohnDude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 15

Re: How to use php with sql server?

Posted 20 March 2015 - 01:22 PM

Thanks no2 for moving my thread.

Thanks for your reply soldier.

SQL server is already installed, I do not need to install it. It is working perfectly.

Found an extension directory on my c: drive. There is also one in the external drive where I have wamp installed. I put the driver both places. I also indicated in the ini file the ext directory:
extension_dir = "b:/wamp/bin/php/php5.5.12/ext/"
extension=php_sqlsrv_55_ts.dll

The setting for startup_errors is enabled. Where do these go?

I would love to debug this. I, however, don't have any information to use to debug. Apart from the posted error I can't find any instructions on what is supposed to be where to check. No stack trace, no registers, no files to examine, no tables to view, no variables to check. Normally when something is debugged there is something to work with.

I installed with all the defaults and changed the only two lines that I have posted. I was hoping I could find someone that knew how to configure php to talk to sql server. Thats what I need to know.

Have a good weekend!
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6582
  • View blog
  • Posts: 30,736
  • Joined: 10-May 07

Re: How to use php with sql server?

Posted 20 March 2015 - 01:27 PM

If sql is running as a service, you should be able to verify this outside of Apache. On a linux system you can use a client to issue commands to it. Not sure how you would do this on Windows.

You have to open port 3306 on the firewall for mysql, in order for the service to bind to a port. This is likely different for mssql.

Do you have a guide that you are using to perform this install? Something we can look at?
Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1869
  • View blog
  • Posts: 5,831
  • Joined: 15-January 14

Re: How to use php with sql server?

Posted 20 March 2015 - 01:51 PM

Quote

The setting for startup_errors is enabled. Where do these go?

I believe that WAMP by default is set to send errors to an error log, so they would go there. There should be a shortcut somewhere in the WAMP control panel to view the various log files. You can also change the log_errors and error_log options in php.ini. I would recommend always using an error log with PHP. You can set display_errors to false to not show error messages in the browser (and use log_errors to log them instead), and the error_log option can be a path to a filename or a few special options (e.g. to use the Windows event log). Make sure that the Apache user has write access to that file. You'll probably also want to set the error_reporting option to E_ALL to show everything.
Was This Post Helpful? 0
  • +
  • -

#13 JohnDude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 15

Re: How to use php with sql server?

Posted 24 March 2015 - 06:24 AM

View Postno2pencil, on 20 March 2015 - 01:27 PM, said:

Do you have a guide that you are using to perform this install? Something we can look at?


I am not using a guide. I wish I had one, I may not even be here if I did. What I did was go here http://www.wampserve...ownload-wrapper and downloaded this file wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b.exe.

I then ran the executable and started following this tutorial http://www.homeandle...uk/php/php.html

SQL Server is installed on a server. I can access the database with SSMS (SQL Server Server Management Studio; a tool that comes with the product). I can access the data from the ERP application. I can write an app from scratch and access the data. I can access the data from Excel through an ODBC connection. SQL Server has been running fine for many years. That is not the issue. The issue is how do I access the data from PHP? It appears I am not loading the driver.

View PostArtificialSoldier, on 20 March 2015 - 01:51 PM, said:

Quote

The setting for startup_errors is enabled. Where do these go?

I believe that WAMP by default is set to send errors to an error log, so they would go there.


I found this information in the log file:
[24-Mar-2015 12:55:36 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'b:/wamp/bin/php/php5.5.12/ext/php_sqlsrv_55_ts.dll' - %1 is not a valid Win32 application.
in Unknown on line 0


I downloaded the 64-bit version of wamp. I suspect there is no 64-bit version of the SQL server driver.
The most recent information I found was from 2012 here: PHP on Windows

Although this post seems to indicate 64-bit can be done. Or perhaps this is only with a local version of SQL server?

So now my questions are:
1) How do I shut down and/or restart the wamp server? Currently I have to reboot everytime I update php.ini. I see no restart option.
2) Can I have both the 64-bit wamp and 32-bit wamp installed on my machine so I can manually start which ever one I need?
3) If I can't have both what is the cleanest way to remove the 64-bit so I can try the 32-bit?

Thanks
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6582
  • View blog
  • Posts: 30,736
  • Joined: 10-May 07

Re: How to use php with sql server?

Posted 24 March 2015 - 06:31 AM

View PostJohnDude, on 24 March 2015 - 09:24 AM, said:

So now my questions are:
1) How do I shut down and/or restart the wamp server? Currently I have to reboot everytime I update php.ini. I see no restart option.

Assuming that you have a WAMP icon in your tray, right click & select restart apache

Or via the command line :
wampserver.exe -quit -id={wampserver}
Then start with wampserver.exe

View PostJohnDude, on 24 March 2015 - 09:24 AM, said:

2) Can I have both the 64-bit wamp and 32-bit wamp installed on my machine so I can manually start which ever one I need?

You really should run the version written for the architecture of your machine. Yes, you can run multiple version of Apache, but you only get one port 80 to bind to. A 2nd one would require an alternative port, &/or to have your initial service to be stopped & free up the port.
[/quote]
Was This Post Helpful? 0
  • +
  • -

#15 JohnDude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 15

Re: How to use php with sql server?

Posted 24 March 2015 - 08:33 AM

View Postno2pencil, on 24 March 2015 - 06:31 AM, said:

wampserver.exe -quit -id={wampserver}
Then start with wampserver.exe


I dont have it in the tray. There would be too much clutter in the tray if I let everything go there that wanted to. Also there is no wampserver.exe. I have a wampmanager,exe which doesn't work with the -quit parameter with or without the id. Thats the problem with free software, quality can often be lacking. Oh well, thanks anyway.


View Postno2pencil, on 24 March 2015 - 06:31 AM, said:

You really should run the version written for the architecture of your machine. Yes, you can run multiple version of Apache, but you only get one port 80 to bind to. A 2nd one would require an alternative port, &/or to have your initial service to be stopped & free up the port.


While true, that works in the perfect world. Often in the real world you can't run the "best" choice of software. Best being latest version, or 64-bit when a 64-bit driver does not exist. If my only choice is 32-bit driver than it is impossible to use the 64-bit version of wamp.

Also I found it impossible to install both versions. It seems when I tried to install the 32-bit version it only installed a subset of directories. When I removed the 64-bit version a complete directory structure was created.

Now it does not work at all. 127.0.0.1 does bring up the wamp server screen. The icon is green. But now this code:
<html>
<head>
<title>PHP - Some Practice</title>
</head>
<body>

PHP test<br>

<?php echo php_uname("m").'<br>'; ?> 

</body>
</html>


Does this:
PHP test
'; ?>

I do have this error in the log file:
[24-Mar-2015 15:08:14 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'b:/wamp32/bin/php/php5.5.12/ext/php_intl.dll' - The specified module could not be found.
in Unknown on line 0

Even though the dll does exist in the ext folder.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2