How to migrate from my WAMP environment to using MSSQL? - php

I have on my local PC installed WAMP environment (using WampServer OS).
Now I would need to change one script from working with MySQL to MSSQL.
I have no idea at all how to proceed, where to download MSSQL, how to install it and how to connect with PHP?
Thank you very much

Download mssql express http://www.microsoft.com/Sqlserver/2005/en/us/express.aspx
Familiarize yourself with one of the great database abstraction layers, for example PDO http://www.php.net/pdo
Refactor your code to use the database abstraction layer
You may need to rewrite the mysql specific things to "standard" SQL

Start by getting Sql Server working. If you are using an old copy of Sql Server (pre 2005) you will need to make sure ntwdblib.dll is present in your System32 directory, and removed from your PHP dll directory (ntwdblib.dll is installed with the enterprise tools, so you may have to install them on your PHP box as well). Once you have Mssql querying properly, start finding where Mssql differs from MySql in your code.
A couple of tips:
The ntwdblib doesn't accept long strings (I think it is limited to 4k, but don't quote me on that). You have to use Stored Procs if you are trying to process a string longer than 4k.
Try to use Sql Server 2005 if you can (see above), the ntwdblib.dll is ancient and has numerous issues.
The PHP site is your friend for mssql.

Related

Laravel 4 on Mac localhost: Cannot find driver for SQLSRV

I'm trying to connect with Laravel 4 to an SQL Server database. I'm getting an error, but I think it's a PHP error, not a Laravel error. It says PDOException: could not find driver. I have the driver set to sqlsrv like another example in the database.php file. Like I said, I think this is a PHP error, not Laravel.
I'm on localhost on my Mac. Do I need to configure something in my php.ini file? I found another article that said I need to comment out a MySQL driver under PDO. I also couldn't find that line in my php.ini. But I need to connect to a MySQL database as well, anyway. What do I need to change in my php.ini file?
I found this line:
pdo_mysql.cache_size = 2000
But commenting it out did nothing even after restarting Apache.
The sqlsrv driver is a windows-only driver according to the PHP Manual (www.php.net/manual/en/sqlsrv.requirements.php). So you can't use that driver on Mac, Linux, Unix or any other operating system than the ones listed on that page.
I assume you are trying to connect to a remote SQL Server somewhere? If you're on Mac, then SQL Server wouldn't be running on your local machine, so I guess the first question is whether or not you have a SQL Server running somewhere.
Second, from the server that is running PHP, you would need to connect using either FreeTDS or ODBC with FreeTDS.
Since Laravel 4 uses PDO, you'll need to install FreeTDS and ODBC. Laravel 4 doesn't use the mssql PHP extension. It uses PDO, so mssql won't get you anywhere with Laravel 4.
If you're serving PHP from your Mac, I would say there are generally 4 different ways you could go:
The Easy, but costly way: Go to (www.macupdate.com/app/mac/23152/openlink-odbc-driver-for-sybase) and download and install OpenLink ODBC driver. This allows you to create new ODBC connections using the Sybase (MSSQL) driver on a XNIX machine. There's an installation walk-through here (wikis.openlinksw.com/dataspace/owiki/wiki/UdaWikiWeb/InstallSybaseExpressOSX). It comes with a 15 day trial, after which you would have to purchase it. As of this writing, it appears to cost somewhere in the range of $30 - $50 depending on the license you choose.
The harder, but free way: Follow http://blog.benjaminwalters.net/?p=10 (slightly outdated) to re-compile FreeTDS support into PHP on Mac (assuming you're using MAMP)
The far better, but also far more work way: Find a Virtual Machine Host software like VMWare Fusion and install a Linux server like Ubuntu 12.4. Make sure you install apache2 and PHP during the install process and then follow the steps here to get FreeTDS working on your new server machine.
Run PHP from the Windows Server that is running SQL Server
Milage will vary with each solution, and there is a learning curve in any case. I've been a PHP developer for a long time, and it took me days to figure out how to get connected to SQL Server for the first time from Linux. Thankfully, there are quite a few walkthroughs out there now, and FreeTDS has come a long way since that time.
Hope that helps. :)
I actually have this running on my setup. My best advice is to get on the Homebrew bus.
https://github.com/josegonzalez/homebrew-php is where you're going to want to look for installing PHP and related items. I don't remember exactly what I did but it wasn't particularly difficult.
First you'll need FreeTDS.
brew install freetds
Then, add Jose's formulas to your homebrew (instructions on the github page). After that,
brew install php5x --with-mssql
brew options php5x
if you need more extensions.
Based on Jon Watson's comment,
I've tried step 2, and it doesn't work. check my issue here https://github.com/laravel/laravel/issues/2180
Step 3 is broken link and I think it's the same with step 2.
I also tried linux ODBC driver for Laravel (there are 2, google it), both doesn't work too. Although my simple test php works in both DBLIB and ODBC (compiled using official microsoft odbc unix driver).
Step 4 definitely works, BUT you must use IIS for best stability.
That leaves step 1. OR,
Ditch Laravel4 and use good ol' AdoDB http://adodb.sourceforge.net/ which I've been using since 2006 and still working superbly (although not modern like Laravel) configure it using tutorial in http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP
I wonder why they don't build sqlsrv driver for linux too....

Trouble using MsSQL with PHP

I am debating between using MsSQL and SQLSrv to connect to SQL Server.
Our platform is running IIS7 on Windows and using PHP. We tried using the MsSQL extension to work but had problems so we decided to use SQLSrv instead since it was easier to get working.
Using MsSQL would be the preferred option because it fits the workflow better and the function names are similar to the ones used in SQL. Is it worth the effort to use MsSQL with PHP in light of the problems we are facing or should we use SQLSrv with the downsides mentioned.
Also, on the MSDN Docs for SQLSrv it mentions that the documentation is for preview and might change. Does that mean the functions could end up changing?
As far as the notice on the documentation all it means is they may change the wording of the document later in its release, I can't promise that the functions will stay the same however nothing on that page leads me to believe otherwise. Here is a Microsoft Wiki article on Accessing SQL Server Databases from PHP is says that the process outlined on that page is compatible with SQL Server 2005, SQL Server 2008, and SQL Server 2008 R2 so the software should not change since it needs to maintain compatibility with older versions of SQL Server.
You may also want to reference that wiki article, it contains step by step setup procedures for MsSQL and SQLSrv which may work for you and fix the issue you have.

How easy is it to use PHP on an MS Server with MS SQL?

I generally use linux servers and program in PHP (plus other associated technologies) with mysql databases. A client has existing ASP sites using MSSQL databases on a Windows server and wants some new sites created on their server using their MSSQL database as the main data source. I have used ASP in the past but much prefer PHP so would rather use this.
I just wanted to confirm that i'm right in saying as long as the server has PHP installed (which it does) then there's no reason I can't create a PHP site on the Windows server and just use different connectors to connect to the MSSQL db (rather than what i would usually use for connecting to a MYSQL db). Presumably (other than the different connect/query code) this would work in pretty much the same way as with a linux setup with mysql and i could then code the site in PHP but use their existing datasource?
I would be extremely grateful if anyone could confirm i'm right in saying this and if there's any other issues that might cause any problems?
Thanks so much for your help as ever,
Dave
You can enable MSSQL support in PHP on Windows. The default Windows PHP install has the php_mssql.dll extension commented out in the php.ini file. Uncomment that line, restart Apache, and it should work.
There is a supporting DLL called ntwdblib.dll that you may need to find a replacement for, as the version included with PHP may be outdated and won't work with newer versions of SQL Server. (I had this problem when setting it up.)
There are two different libraries to connect to SQL Server from PHP:
The legacy mssql extension, which has been discontinued on Windows
Microsoft's sqlsrv extension, which only works on Windows
I've basically worked with the second one and I can say it's a very interesting product. It's robust, it has a very nice interface and it's totally up-to-date. But what I like most is that it has very nice features. E.g.:
It can return dates as PHP DateTime objects
It provides a PDO driver
The only drawback of using SQL Server is that there isn't an easy way to write a cross-platform app but I understand it isn't problem in your case.
Yes, it is rather straight forweard.
You need to have the mssql library activated in php.ini. Which can be troublesom.
MSSSQL docmentation

PHP to SQL Server connection

I want to connect to a SQL Server 2000 from PHP installed in a unix platform (CentOs) and to be able to run queries (SELECT and UPDATE). The solution must support UTF-8 data.
As search through the web, I find out that there exists many different approaches (freeTDS, unixODBC, ODBTP, Easysoft ODBC, Easysoft ODBC-ODBC Bridge, PDO).
Could someone point me to the best solution for the specific connection?
PDO is the preferred library a.t.m, with PDO_DBLIB (at least, for Linux), see here. if you need it portable between Linux & Windows, use PDO_ODBC.
The fact that PDO_DBLIB was (and still is) experimental discourage me from using it. I decided to start testing a solution using freeTDS, that seemed to be the simpliest method. It turned out to function excellent, for that specific connection's case and I implemented the final application using it.

Connecting to Oracle using PHP

How do I connect to a remote Oracle database instance from PHP?
I need to query (read only) the remote Oracle database for some information; do I need to have an Oracle Instant Client installed?
Is the OCI extension for PHP enough?
From PHP Manual
You will need the Oracle client libraries to use this extension.
The most convenient way to install all the required files is to use Oracle Instant Client, which is available from Oracle's site
The best manual for using PHP with Oracle is Underground PHP Oracle Manual. Periodically updated. For example last update describe new cool OCI (Oracle Call Interface) features. I found it by accident and since then has never regretted. Start from that good manual.
there are a couple of steps you need to go through to make this work.
First, you need to install the oracle driver for whatever OS you have. Then, create a DSN for odbc to use to connect the php function call to the oracle database. On windows, you can find this on the Control Panel -> ODBC Sources
Once you have done this, restart the DB, the web server and then you should be able to test it all with this:
odbc_connect($dsn,$user,$pass);
If you have linux, the same steps are needed but I'm not sure how you create a DSN in unix.
I saw this in the "Notes" section of the PHP documentation:
If you're using PHP with Oracle Instant Client, you can use easy connect naming method (...)
So I think it's rather clear that you can connect to an Oracle DB without the Oracle Instant Client, using only the PHP Oracle extension.
If you're attempting to connect to oracle on ubuntu with PHP, the following links have been more than helpful:
A) http://pecl.php.net/bugs/bug.php?id=9253
That's the real-workhorse one - it gives you just about all the data you need.
B) http://fabrizioballiano.net/2008/01/26/how-to-install-php-pdo_oci-on-ubuntu-gutsy/
This is also helpful for details of things that need to be installed for oracle to work with ubuntu.
If you're using it with PHP, you'll need to make sure that the TNS_ADMIN and ORACLE_HOME environment variables are available for apache's user - there's a file named 'envvars' in the apache2 directory where you can set these. (For my own ease of use, I have the two point to the same directory.)

Categories