August 23,2016
Connecting Via Get Method
There are two ways to connect to MYSQL via PHP page. The first one is called Get method. We will use HttpGet and HttpClient class to connect. Their syntax is given below −
URL url = new URL(link); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(link));
After that you need to call execute method of HttpClient class and receive it in a HttpResponse object. After that you need to open streams to receive the data.
HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
Connecting Via Post Method
In the Post method, the URLEncoder,URLConnection class will be used. The urlencoder will encode the information of the passing variables. It’s syntax is given below −
URL url = new URL(link); String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8"); data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); URLConnection conn = url.openConnection();
The last thing you need to do is to write this data to the link. After writing, you need to open stream to receive the responded data.
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write( data ); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Example
The below example is a complete example of connecting your android application with MYSQL database via PHP page. It creates a basic application that allows you to login using GET and POST method.
PHP – MYSQL part
In this example a database with the name of temp has been created at 000webhost.com. In that database, a table has been created with the name of table1. This table has three fields. (Username, Password, Role). The table has only one record which is (“admin”,”admin”,”administrator”).
The php page has been given below which takes parameters by post method.
<?php $con=mysqli_connect("mysql10.000webhost.com","username","password","db_name"); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $username = $_POST['username']; $password = $_POST['password']; $result = mysqli_query($con,"SELECT Role FROM table1 where Username='$username' and Password='$password'"); $row = mysqli_fetch_array($result); $data = $row[0]; if($data){ echo $data; } mysqli_close($con); ?>
Android Part
To experiment with this example , you need to run this on an actual device on which wifi internet is connected.
| Steps | Description |
|---|---|
| 1 | You will use Android studio IDE to create an Android application and name it as PHPMYSQL under a package com.example.phpmysql. |
| 2 | Modify src/MainActivity.java file to add Activity code. |
| 3 | Create src/SiginActivity.java file to add PHPMYSQL code. |
| 4 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
| 5 | Modify res/values/string.xml file and add necessary string components. |
| 6 | Modify AndroidManifest.xml to add necessary permissions. |
| 7 | Run the application and choose a running android device and install the application on it and verify the results. |