Wednesday, January 25, 2012

Connect with Microsoft Sql server from PHP in xampp

Hello,

Recently I was working on a project in which requirement was to get data from Microsoft Sql Server from a PHP script. So this blog is about accessing MS Sql server from PHP using PDO object.

First thing is to add necessary extensions. Go to following site and download drivers for MS SQL server.

http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

Once you extract it it will have number of dll files Now you have to identify correct DLL files for your system. This drivers are compatible with PHP version 5.3 . Now go to your XAMPP installation and search for php.dll
It will display correct PHP dll you have.

1) If you have php.dll then move following files to xampp/php/ext directory.


php_sqlsrv_53_nts_vc9.dll
php_pdo_sqlsrv_53_nts_vc9.dll

2) If you have php5ts.dll then move following files to xampp/php/ext directory.



php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll

above files should be used if your PHP version is compiled with Visual C++ 9.0 . Else following files should be used.



1) If you have php.dll then move following files to xampp/php/ext directory.


php_sqlsrv_53_nts_vc6.dll
php_pdo_sqlsrv_53_nts_vc6.dll

2) If you have php5ts.dll then move following files to xampp/php/ext directory.



php_sqlsrv_53_ts_vc6.dll
php_pdo_sqlsrv_53_ts_vc6.dll

Normally it should be VC 9 files.

Now we have to load files that we added recently. Open the php ini file and add entry in the area of dynamic extensions as follow.

 extension=php_sqlsrv_53_nts_vc9.dll
 extension= php_pdo_sqlsrv_53_nts_vc9 .dll

Save the ini files and restart you Apache from either XAMPP control panel or from Windows Services Console.

After that you can check php info as follow.

<?php

phpinfo();

?>

Check the pdo support. It should display sqlsrv now. That means modules are loaded successfully and drivers are available now. Use following code to connect to MS SQL server using sqlsrv


<?php
$serverName = " serverName\instanceName ";
$connection = array( "Database"=>"dbName");
$conn = sqlsrv_connect( $serverName, $connection);

if( $conn ) {
     echo "Connection established.
";
}else{
     echo "Connection could not be established.
";
     die( print_r( sqlsrv_errors(), true));
}
?>


That's it and you should be able to query Microsoft Sql Server now. Hope this helps you. This works with Microsoft Sql Server 2005, 2008 and 2008 R2.











Friday, January 20, 2012

Working with Radio buttons in Android

In this blog I am going to explain how to work with Radio Buttons in Android application. As we all know we can add controls using layout XML. Here I will explained that how to check status of radio buttons and clear radio buttons status etc.

Following is the example of how we can add radio button using layout XML.


<RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroupId">
                <RadioButton android:id="@+id/radio1"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Radio 1" />
                <RadioButton android:id="@+id/radio2"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Radio 2" />
</RadioGroup>

Following is the code to get reference of Radio group and iterate through it to get the status in activity class.


RadioGroup  _radioGroup = (RadioGroup) findViewById(R.id. radioGroupId);        
count =  _radioGroup.getChildCount();          

for (int i=0;i>count;i++) {
         View radio =  _radioGroup.getChildAt(i);
         if (radio instanceof RadioButton) {
              if(((RadioButton) radio).isChecked()){
              }
        }
}

Following is the code to reset status of Radio group.


RadioGroup  _radioGroup = (RadioGroup) findViewById(R.id. radioGroupId);
_radioGroup.clearCheck();

This will reset the radio group.

Hope this posts help you.




Monday, January 2, 2012

Beginning Android Programming

Hello,

Here is my second blog on Android and this is some what different blog. Here I am going to share some useful tips for beginners of android. These are the issues which I faced as a beginner in Android.

1) How to setup your phone for testing an Application.

Normally android phones does not allow to test application but you can configure it. Go to Settings->Applications

There will be a check box here Unknown Sources (Allow installation of Non market applications). It must be checked in . If not it will only application installation from android market.

After that Go to Settings->Applications->Development. Here there will be another check box USB debugging (Debug mode when USB is connected) It must be checked in. If not, it will not catch an exception in LogCat of eclipse IDE.

2) How to uninstall application from your virtual device

There might be the case that while you are testing with virtual device, you need to re install application. For that you have to use Android ADB. Keep your virtual device on  while working through adb. That you can find in Android android-sdk\platform-tools Go to command prompt and navigate to above location. After that type following command. 
adb shell. 

It will open an adb shell. Go to data/app folder here you will find application apk file. Remove apk file using following command.
rm application_name.apk

This will remove application from virtual device. If you want to remove application from phone you can do it via Applications->Manage Applications.

3) How to prevent activity from restarting on orientation change

This might be one of the common issue every beginners faces. You have declared an activity in in your manifest file. While testing in phone when you change orientation, application restart. To prevent this you need to add following attribute to your activity declaration in your manifest file.

android:configChanges="orientation"

This will save application data and state on change of orientation and will restore it back.

4) How to start new activity

We normally call it navigation. You have a one screen in one activity. On some user actions you want to load another screen. For that first you need to declare all the activities in your manifest file. Following is the code to start new activity

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(i);

5) How to pass data between two activities 

It might be the case that you need to pass some parameters from one activity to another. Following is the code you need to add in intent of new activity.

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
Bundle bundle = new Bundle();
bundle.putString("param", "value");
i.putExtras(bundle);
startActivity(i);

In new activity you can access this parameter in onCreate method as follow.

Bundle bundle = this.getIntent().getExtras();
param = bundle.getString("param");


I hope this helps you.