Android PHP MySQL Login Tutorial

Android has many options to choose which database you want to use in the applications. SQLite database only good for data that store in the local device, it is not possible to use it globally. To share and use one database everywhere then we should web server to achieve it which Android PHP MySQL is a good choice. Moreover, android application is able to connect PHP to use and do some modification in MySQL database. Mysql is an open source relational database management system (RDMS), it is widely used over the world. In this tutorial, you will learn how to connect MySQL in the android application and do a simple login to test out the result.


  1. Android Studio
  2. Apache Web Server and MySQL (Recommend to download XAMPP)

First, you need to open XAMPP and start Apache and MySQL.

Create a Login table

Go to PHPMyAdmin in your localhost, eg (//localhost/phpmyadmin/). Add a new database table name it as “androiddb“.

android php mysql-1



After that, add a new “login” table. The login table has two rows which are username and password. Follow the following field detail to create it.


Insert a record, I set the username is “user” and password is “pass” so that later can test with this username in the mobile application.


Create a New PHP Project

Add a new project name “php-android-login” and add two new PHP file. The first is config.php and second is login.php.

Edit config.php file

This file is to connect to the MySQL database, you should enter your server name. In my situation, I use localhost so I enter localhost. Enter the server username and password, my password empty cause I dint set any password. Next, enter the database name “login” that created just now. All information should be correct so that you able to connect the server. You can test run the config.php to test whether connect a not, if not connected it will prompt “Cannot connect to db”.

Edit login.php file

Copy below code into your login.php. In short, the code means if username and password match to your login table record, the result will become true. Else, will become false.

Creating a New Android Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “PhpMysqlExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit activity_main.xml layout

I add two text view in this layout to determine this is the main page. You can copy the following code.

Create a Login Activity

Right click package name > New > Activity > EmptyActivity. After that, enter your activity name “LoginActivity”. Click finish button.

Edit activity_login.xml layout

The following code is activity_login.xml, I add two EditText for username and password input. Then, I add a button to do background process to enter main activity.

Create a New class

Right click package name > New > Java class and name it as “AsyncLogin”.

Edit class

The below source code is Asyntask is to perform background process, the url “//localhost/php-android-login/login.php” should be correct, otherwise, you cannot connect the application to the PHP file. The following URL is my local path, please check your local path correctly.

Edit class

The following code is class copy and pastes it.

Edit AndroidManifest.xml

You should add two permission which is INTERNET and ACCESS_NETWORK_STATE, else you will get the socket error. Besides, set LoginActivity to first launch.

Run Your Project

Now, you can run your project and test whether can log in to the main activity.

(Android PHP MySQL Login)

Source Code




(Visited 7,656 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

31 Responses

  1. DF says:

    if i insert your code into my android studio the app on my phone opens, and i can enter a password and user name. but when i press the login button the app closess on my phone

  2. Kristofer Ng says:

    did you download xampp to setup your web server?

  3. jane says:

    hey hi,
    I’m getting SocketTimeoutException: timeout. I’m running the app on nexus device.
    But if I try to put the url in the browser open up to show connected.
    Can you help ?

    • Kristofer Ng says:

      url = new URL(“http://localhost/php-android-login/login.php”);
      for the url “localhost” change to your local ip address,
      it should be work.

      you can check your local ip address via command prompt by typing “ipconfig”

  4. virat says:

    When I run the code, I get this null pointer in the logcat file :

    java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.Resource&Theme android.content,Context.getTheme()’ c

    How to rectify this?

  5. david says:

    HI, that for the guide.
    getting error as follow:
    java.lang.NullPointerException: Attempt to invoke virtual method ‘android.content.res.Resources$Theme android.content.Context.getTheme()’ on a null object reference

    crash upon clicking login button.
    Using Wammp server. i able to connect through android phone with other project.
    How to solve this?

  6. Rulillada says:

    url = new URL(“http://iplocal:8000/php-android-login/login.php”);
    networkstate and internet permissions

  7. Rulillada says:

    private LoginActivity loginActivity;
    private ProgressDialog pdLoading; ——-)))))))
    private HttpURLConnection conn;
    private URL url = null;

    public AsyncLogin(LoginActivity loginActivity) {
    this.loginActivity = loginActivity;

    protected void onPreExecute() {
    pdLoading = new ProgressDialog(loginActivity); —-))))))) here
    //this method will be running on UI thread


  8. Pieter Du Toit says:

    I created an APK file and installed on my phone. It goes directly to login successful without displaying the login page

  9. Jay says:

    Put the source code and we will download

  10. zaid says:

    where code?

  11. Pablo says:

    i got problems with ProgressDialog it dosen’t work in
    ProgressDialog pd = new ProgressDialog(loginActivity);

  12. I have sendUserActionEvent() mView == null ………error

  13. Appoorve says:

    I have a query… I hd not followed your code bt approached in some other way and getting registration success toast but there is no data available in the database

  14. Appoorve says:

    If u r interested to help me out…. Then I should send u the screenshots

  15. Jessica says:

    Hi, just want to comment a typo, you named your database as ‘androiddb’ at the beginning of the post but then you put ‘login’ as your database name in config.php

  16. Zakir Khan says:

    i got the message as application stopped working

  17. Only about a week to ten days ago, we spent an hour and a half together talking about his dreams for Lebanon and for a peaceful Middle East.

  18. I’ve been browsing online greater than three hours as of
    late, but I never found any fascinating article like yours.

    It’s beautiful value sufficient for me. In my view, if all web owners and bloggers made just right content material as
    you probably did, the web will be a lot more useful than ever

  19. Muhammad says:

    This helped alot thanks.

  20. Mozeago says:

    I have tried to grasp this concept from top devs but I couldn’t, all I get was errors am Happy your code worked pretty well, Good work,Keep up. Can you kindly do one on taking a photo and saving it locally to phone using sqlite database. Item, description and image (saved locally).

  21. mywebsite says:

    Thanks designed for sharing such a good opinion,
    paragraph is nice, thats why i have read it entirely

  22. Rubi says:

    How can I do the same if my config file is in godaddy hosting.

Leave a Reply

Your email address will not be published. Required fields are marked *