PHP MYSQL-Android Studio
PHP MYSQL-Android Studio
• Isi dengan:
• INSERT INTO table1 (FirstName, LastName, Age) VALUES ('admin',
'admin','adminstrator')
PHP - GET and POST methods
• PHP is also used to fetch the record from the mysql database once it
is created. In order to fetch record some information must be passed
to PHP page regarding what record to be fetched.
• The first method to pass information is through GET method in
which $_GET command is used. The variables are passed in the url
and the record is fetched.
File PHP
<?php
$con=mysqli_connect("example.com","username","password","database name");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_GET['username'];
$password = $_GET['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 - Connecting MYSQL
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.
OutputStreamWriter wr = new
OutputStreamWriter(conn.getOutputStream());
wr.write( data );
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
Example
• 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").
Example
<?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.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameField = (EditText)findViewById(R.id.editText1);
passwordField = (EditText)findViewById(R.id.editText2);
status = (TextView)findViewById(R.id.textView6);
role = (TextView)findViewById(R.id.textView7);
method = (TextView)findViewById(R.id.textView9);
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameField = (EditText)findViewById(R.id.editText1);
passwordField = (EditText)findViewById(R.id.editText2);
status = (TextView)findViewById(R.id.textView6);
role = (TextView)findViewById(R.id.textView7);
method = (TextView)findViewById(R.id.textView9);
}
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
return sb.toString();
} catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
}
Gradle
• Add the following content to build.gradle and rebuild the whole
project.
android {
useLibrary 'org.apache.http.legacy'
}
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
activity_main.xml. android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="44dp"
android:ems="10" >
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <requestFocus android:layout_width="wrap_content" />
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" </EditText>
android:layout_width="match_parent"
android:layout_height="match_parent" <TextView
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/textView1"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:layout_width="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin" android:layout_height="wrap_content"
tools:context=".MainActivity" > android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
<EditText android:text="@string/Username" />
android:id="@+id/editText2"
android:layout_width="wrap_content" <TextView
android:layout_height="wrap_content" android:id="@+id/textView3"
android:layout_alignRight="@+id/editText1"
android:layout_width="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="25dp" android:layout_height="wrap_content"
android:ems="10" android:layout_alignParentTop="true"
android:inputType="textPassword" > android:layout_centerHorizontal="true"
</EditText> android:text="@string/App"
android:textAppearance="?android:attr/textAppearanceLarge" />
activity_main.xml.
<TextView
<TextView android:id="@+id/textView4"
android:id="@+id/textView7"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView5" android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6" android:layout_alignLeft="@+id/textView8"
android:text="@string/Role"
android:textAppearance="?android:attr/textAppearanceMedium" android:layout_below="@+id/button1"
android:textSize="10sp" />
android:layout_marginTop="86dp"
<TextView
android:id="@+id/textView5" android:text="@string/LoginStatus" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView6"
android:layout_marginTop="27dp"
android:layout_toLeftOf="@+id/editText1" <TextView
android:text="@string/LoginRole" /> android:id="@+id/textView6"
<TextView android:layout_width="wrap_content"
android:id="@+id/textView8"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView6" android:layout_alignTop="@+id/textView4"
android:layout_alignLeft="@+id/textView5" android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp"
android:text="@string/method" /> android:text="@string/Status"
activity_main.xml. <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMe android:layout_height="wrap_content"
dium"
android:textSize="10sp" /> android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
<TextView
android:id="@+id/textView9" android:layout_alignLeft="@+id/textView2"
android:layout_width="wrap_content" android:onClick="login"
android:layout_height="wrap_content" android:text="@string/LoginGet" />
android:layout_alignBottom="@+id/textView8"
android:layout_alignLeft="@+id/textView6"
android:text="@string/Choose" <TextView
android:textAppearance="?android:attr/textAppearanceMe android:id="@+id/textView2"
dium" android:layout_width="wrap_content"
android:textSize="10sp" />
android:layout_height="wrap_content"
<Button android:layout_alignBaseline="@+id/editText2"
android:id="@+id/button2" android:layout_alignBottom="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:text="@string/Password" />
android:layout_toRightOf="@+id/textView6"
android:onClick="loginPost"
android:text="@string/LoginPost" /> </RelativeLayout>
Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PHPMYSQL</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Username">Username</string>
<string name="Password">Password</string>
<string name="LoginGet">Login - Get</string>
<string name="LoginPost">Login - Post</string>
<string name="App">Login Application</string>
<string name="LoginStatus">Login Status</string>
<string name="LoginRole">Login Role</string>
<string name="Status">Not login</string>
<string name="Role">Not assigned</string>
<string name="method">Login Method</string>
<string name="Choose">Choose Method</string>
</resources>
AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phpmysql" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.phpmysql.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Coba - wireframe