Android phpMysql connection
First create a database named “mydatabase” in MySql. Then create a table “tbl_user” with three fields (id, username and password).
The next step is to create a php page which will communicate between the MySql database and the android application.
For this create a “Connections.php” page as shown below. It will set up a connection to the database with the username and password
$hostname_localhost ="localhost"; $database_localhost ="mydatabase"; $username_localhost ="root"; $password_localhost =""; $localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
Then the main PHP file is to be created. Actually the android application will call this php file along with the data(username and password ) and this code will check the database. If the details is valid then it will return “Y” and otherwise return “N”.
Here the data is passed as POST method. We can also use GET method where the data is passed along with the url . Android support both these two features.
<?php require_once('yourfolder/Connections.php'); mysql_select_db($database_localhost,$localhost);
$useremail = $_POST['UserEmail'];
$password = $_POST['Password'];
$query_search = "select * from tbl_user where username = '".$useremail."' AND password = '".$password. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
if($rows --> 0) { echo "Y"; }
else {echo "N"; }
So by now we created php and the database part. Then we have to focus on android section. For this first create a xml file which will be our login screen.The screen look like this.

The layout of the xml is displayed below.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff2a1703"
android:fadingEdge="horizontal">
<TextView
android:id="@+id/text"
android:text="manage your projects..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif"
android:textStyle="italic"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dip"
android:textSize="20sp" >
</TextView>
<EditText
android:id="@+id/username"
android:layout_width="213dip"
android:layout_marginTop="60dp"
android:layout_below ="@+id/text"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:hint="username"
android:gravity="center"
android:textSize="18sp"
android:typeface="sans"
android:textStyle="italic">
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="211px"
android:layout_height="wrap_content"
android:hint="password"
android:textSize="18sp"
android:typeface="sans"
android:textStyle="italic"
android:gravity="center"
android:password="true"
android:layout_marginTop="20dp"
android:layout_below ="@+id/username"
android:layout_centerHorizontal="true">
</EditText>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me"
android:layout_marginTop="10dp"
android:layout_below ="@+id/password"
android:layout_centerHorizontal="true">
</CheckBox>
<Button
android:id="@+id/login"
android:layout_width="141px"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="22sp"
android:typeface="serif"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_below ="@+id/check"
android:layout_centerHorizontal="true">
</Button>
</RelativeLayout>
We now create the java file . This file should contain the 3 sections
1. Store the preference when the checkbox is checked
2. Establish a connection between this file and the php file
3. Validate the user
package com.ar.mydatabaseProject.pack;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class mydatabaseProject extends Activity
{
/** Called when the activity is first created. */
Button login;
String name="",pass="";
EditText username,password;
TextView tv;
byte[] data;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
InputStream inputStream;
SharedPreferences app_preferences ;
List<NameValuePair> nameValuePairs;
CheckBox check;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
check = (CheckBox) findViewById(R.id.check);
String Str_user = app_preferences.getString("username","0" );
String Str_pass = app_preferences.getString("password", "0");
String Str_check = app_preferences.getString("checked", "no");
if(Str_check.equals("yes"))
{
username.setText(Str_user);
password.setText(Str_pass);
check.setChecked(true);
}
login.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
name = username.getText().toString();
pass = password.getText().toString();
String Str_check2 = app_preferences.getString("checked", "no");
if(Str_check2.equals("yes"))
{
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("username", name);
editor.putString("password", pass);
editor.commit();
}
if(name.equals("") || pass.equals(""))
{
Toast.makeText(mydatabaseProject.this, "Blank Field..Please Enter", Toast.LENGTH_LONG).show();
}
else
{
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/android/user_validate.php");
// Add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));
nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
data = new byte[256];
buffer = new StringBuffer();
int len = 0;
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
}
inputStream.close();
}
catch (Exception e)
{
Toast.makeText(mydatabaseProject.this, "error"+e.toString(), Toast.LENGTH_LONG).show();
}
if(buffer.charAt(0)=='Y')
{
Toast.makeText(mydatabaseProject.this, "login successfull", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(mydatabaseProject.this, "Invalid Username or password", Toast.LENGTH_LONG).show();
}
}
}
});
check.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on clicks, depending on whether it's now checked
SharedPreferences.Editor editor = app_preferences.edit();
if (((CheckBox) v).isChecked())
{
editor.putString("checked", "yes");
editor.commit();
}
else
{
editor.putString("checked", "no");
editor.commit();
}
}
});
}
public void Move_to_next()
{
// startActivity(new Intent(this, zzz.class));
}
}
At last don’t forget to put the permission in Manifest file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Note : if you have any problem in working with this post, please check this more simple version here.
Here is another more detailed explanation.
How to create Simple Login form using php in android? – Connect php with android.