Share This Article
You might not hear or not that you can use PHP for your Android App Development. We can use PHP as the back-end for our Android Applications, and trust me they work flawlessly with each other. Any Android app that needs account log-in and registration can efficiently utilize PHP on its back-end.
Almost every Android applications need essential user data, for that they have to integrate log-in and registration options that provide user’s contact and other details to the back-end. So, let’s explore how you can use PHP on your backend of the Android Application.
Essentially we have to make a log-in and register interface for the android application so we will set up a local PHP server and MySQL database to build PHP Web Development.
PHP for Android
PHP is best known for its incredible and versatile use in Web Development but we can utilize it in many more ways. Today, We are going to use PHP in android app development and how will it going to benefit us. PHP’s best use in Android Applications is in the back-end and database. We are going to guide you through setting up a local server and MySQL Database that will help you in managing user registration and other details.
PHP for Android App Back-end
Let’s get into details about how to use PHP on the back-end of your Android Application. Follow our simple step-by-step guide to set PHP local server and MySQL database and connect them to the back-end of your Android application.
Login Registration Interface
Firstly, we are going to create a back-end web server for the android application. You can use any operating system and use XAMPP. It will help you set up a local Apache Web Server and MySQL database.
XAMPP Server Set-up
Installing XAMPP is pretty easy, just make it google, and then you can get a one-click installation file for any operating system from their website.
After successful installation, you can test it by running or calling http://localhost. It will open a new screen and check admin by http://localhost/phpmyadmin.
You can create a folder there like test and copy your files or test files and you can open them via http://localhost/test_android/test.php
Now, our local server is good to go and we need to set up the database and for PHP MySQL is the best.
Database Set-Up
To set up call phpMyAdmin by visiting http://localhost/phpmyadmin and selecting the database tab from the left top header.
Give it a name you like or your project name and create the database. It can be seen and accessed from the left slide bar. Now create a User table for your App.
Use the given codes to create and set the MySQL database.
Create DataBase
First, you have to create the database and it can be done via the above-mentioned method or you can simply run this code and it will create a database in a single click.
<?php
$con=mysqli_connect("example.com","username","password");
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql)) {
echo "Database my_db created successfully";
}
?>
Create Table
After creating the database you have to create a user table which can be done by running this code.
<?php
$con=mysqli_connect("example.com","username","password","my_db");
$sql="CREATE TABLE table1(Username CHAR(30),Password CHAR(30),Role CHAR(30))";
if (mysqli_query($con,$sql)) {
echo "Table have been created successfully";
}
?>
Insert values to Tables
Tables are useless until they have some values in them. Use this code to add values to your user tables in your database.
<?php
$con=mysqli_connect("example.com","username","password","my_db");
$sql="INSERT INTO table1 (FirstName, LastName, Age) VALUES ('admin', 'admin','adminstrator')";
if (mysqli_query($con,$sql)) {
echo "Values have been inserted successfully";
}
?>
Get & Post method
We can insert values by $_Get and $_Post commends.
<?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);
?>
This part is a bit tricky, We suggest you Hire PHP Developer to help you with this. They will make this technical part easier for you can go back to your android app development.
Connect PHP to MySQL database
To connect PHP to the database you will need to input three values which are Hostname, MySQL username, and password.
To check your connection run http://localhost/test_and/test-connect.php . it will give you an output, if not then restart the XAMPP.
Android Login & Registration
Now we successfully connected MySQL and backend; You will need to build a sign-in/login and registration interface.
Test some random users by entering random users to the tables to get the idea of our work and you are good to use this database and back-end made on PHP. All you need is to build the login and registration part on your android application. or you can Hire Mobile App Developers to get that done professionally.