Wednesday, July 15, 2015

MAMP - Install MSSQL Extension

Recently in one of my project I was trying to connect to MS SQL server from PHP and it was showing error  "Call to undefined function mssql_connect()". That means you are missing MSSQL extension and you have to install it. In this blog I am going to explain how to do this.

First of all you will need FreeTds. FreeTds is the library for linux and unix to communicate with MS SQL server. You can download latest stable release from this link

ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz

After downloading it, extract it and go to that directory in terminal and run following command.

$ sudo ./configure --prefix=/private/etc/freetds --with-tdsver=8.0 --sysconfdir=/private/etc/freetds/conf
$ sudo make
$ sudo make install

This will install FreeTds on your system. Now we have to compile MSSQL extension for your PHP version. For that first check which PHP version are you using in MAMP. That you can check in Server -> PHP tab


Check your version and download source code of that PHP version.


Extract it and go to this directory in terminal.

Now run following commands in terminal

$ ./configure --with-php-config=/usr/bin/php-config
$ make

Now go to MSSQL extension directory in PHP source code.

$ cd ext/mssql/
$ /usr/bin/phpize
$ ./configure --with-php-config=/usr/bin/php-config --with-mssql=/private/etc/freetds
$ make

Above command will generate MSSQL module so now go to module directory.

$ cd modules/

Now we have to copy this generated module to MAMP's current PHP extension directory. For that open the ini file of your PHP version. In my case I was using PHP 5.5.3 so my ini file was located at.

/Applications/MAMP/bin/php/php5.5.3/conf/php.ini

Open this file and search for "extension_dir" and you will find following line.

extension_dir = "/Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/"

so this is your extension directory. We have to copy module here. So now go to terminal and run following command.

$ cp mssql.so /Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/

This will copy mssql.so in your extensions directory. Now we have to add this extension in php.ini file.

Now in php.ini file find for the word "; Extensions" below that there are list of extensions. Add following line to the end of the list.

extension=mssql.so

Save the file. Restart your MAMP and run the phpinfo(). You must see following section there.


But wait it won't be there. That means there is something wrong. After wasting almost an hour I figure out that MAMP is actually using different ini file.  So we have to update that. For that go to 

/Applications/

Right click on your MAMP or MAMP Pro icon (if you are using MAMP Pro) and select "Show Package Contents" Now go to Contents/Resources  and open ini file of your PHP version. Now in that file find for the word "; Extensions" below that there are list of extensions. Add following line to the end of the list.

extension=mssql.so

Save the file. Restart your MAMP and run the phpinfo(). You must see following section there.


That's it and now you can connect to MSSQL server with following code.

$con = mssql_connect('MYSQLSERVER', 'username', 'password')
or die('could not connect to server.");

I hope this helps you and saves your time.

Saturday, July 4, 2015

Android ClassNotFound Exception

Today when I was working on my Android project, I ran into strange issue. When I start my app, it crashes with ClassNotFound exception. It was unable to found Main activity class.

I tried so many things like clean - rebuild, removing all the references and add them again, changing target APIs. But it was not working. App never started.

After doing failed attempt for almost half an hour, it came to my attention that src folder in project was a simple folder. There was no package sign on it and when I explore it, all my packages where turned into folders and sub folders and that was the issue. My src folder was not a sources folder and because of that it was unable to found a class.

So to solve this issue.  Right click on src folder Select Java Build Path - > Use as a source folder.

And that's it all the folders and sub folders in the src folder turned into packages and application was working fine. Hope this helps you and save your time.