Wednesday, September 9, 2015

How to connect Android Application with Microsoft SQL Database using PHP and retrieve data. (Section 1)

Section 1 I’ll discussed how to create odbc connection and write php code to get information from database to URL.

Part I: Create an ODBC connection.

Start MSSQL server Management studio.

Select SQL Server Authentication, Login Username and Password. Click Connect.

Left Side panel you can see Databases you created already.

Open the Administrative Tools icon in your Control Panel.

Double-click on the Data Sources (ODBC) icon inside.

Choose the System DSN tab and Click on Add in the System DSN tab.

Select the SQL Server Driver. Click Finish.

In the next screen, give Data Source Name. (This is very important. You should remember this) and select your sql server.

Select second radio button. Give Login ID and Password (It should be same as your SQL server Login ID and Password)

If you connect with MSSQL server using Windows NT authentication, Select first radio button. Then you don’t need to give any username or password.

 Click Next and choose Database.

Click Next and then Finish.
Then it appears Test Data source window.

If you successfully connect, you will able to see this prompt.


Part II: write PHP code to retrieve information from MSSQL Server

Start wamp server. Go to www folder and create new folder and rename it.create .php file inside that folder and write below code into it.

The odbc_connect() function is used to connect to an ODBC data sourceThe function takes four parameters: the data source name, username, password, and an optional cursor type.
The odbc_exec() function is used to execute an SQL statement.
The following example creates a connection to a DSN called “firstsql”, with my username and password.and also I wrote sql query to select information from Dept table which located in AndroidAppDB database.
Json encode is used to encode data to Jason array.

After that run http://localhost:81/my%20project/first.php.You will able to see this.

Next section I’ll create android app to get information from this URL. To be continuing…

1 comment: