Creating MySQL Database
- Go to your phpmyadmin database (I am using my localhost from wamp server).
- Create a simple table named users of 3 columns (userid, username and password). userid will be the primary key and make it auto increment.
Creating PHP Script
- Create a php script. Our script will get two post values username and password from our android app.
- Then we will check that the given username or password exist or not?
- If exist we will echo a message success
- Else we will echo a message failure
login.php
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','user');
$con = mysqli_connect(HOST,USER,PASS,DB);
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from users where username='$username' and password='$password'";
$res = mysqli_query($con,$sql);
$check = mysqli_fetch_array($res);
if(isset($check)){
echo 'success';
}else{
echo 'failure';
}
mysqli_close($con);
?>
My userprofile.java file
package com.example.android_php;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class UserProfile extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.USER_NAME);
TextView textView = (TextView) findViewById(R.id.textView3);
textView.setText("Welcome "+username);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Now you can test your application.
Total Page View
Stats For Free
No comments:
Post a Comment