Get 20% off on all New HostGator Hosting plans with Coupon: SNAPPY.

Android Tutorials : Connecting your Android Application to a Database using PHP and MySQL


Most of the Android applications will have a need to fetch the data from a database stored in a web server. An android application communicates with PHP with necessary parameters and PHP will in turn communicate with MYSQL database to fetch the data and return it to an android application. In this tutorial, we will show you how to integrate PHP and MYSQL with an android application. In this example, our android application fetches all the upcoming movies stored in a database and populates them in a RecyclerView. Each movie can be clicked to get a pop up menu which will have several options, such as Add to Favorites, Add to Watchlist etc.

Project on Github

Let’s dive right into creating an android ‘Upcoming Movies’ application. The steps are described in 4 detailed yet simple parts :

Part ! : Includes steps to set up XAMPP

1. XAMPP is a free and open source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP. XAMPP will install Apcahe, MySQL and PHP. Apcahe is a web server software. It is commonly used to run Php. MySQL is a Database Engine. You can download the latest version of XAMPP from this link.


2. Once the XAMPP setup file is downloaded, you can go through the installation process. Its recommended to install the XAMPP software in C:\xampp folder. Once the installation is complete, you can launch the XAMPP control panel. This is how it looks.

XAMPP Control Panel

3. Now you need to start both apache server and MySQL. Once you start them, the label will turn green as shown below. It means you have successfully started the server and the MySQL client.

XAMPP Control Panel

4. Now you can proceed with the creation of a database by using PHPmyadmin. XAMPP comes with a web based package known as PHPmyadmin for creating databases, browsing the data in the database, executing your SQL queries and much more. To access PHPmyadmin, click on “Admin” button(next to MySQL start/stop button) as shown below.

Click on Admin buttin

5. It will open the PHPmyadmin client in your default web browser. It looks as shown below.


6. Now we need to create a database and a table in it, to store all the data that will be accessed from our Android App. I am using phpmyadmin to create a database, a table and insert values into it. To do that , you need to click on ‘SQL’ tab of your phpmyadmin client and run the following SQL query.


7. We need 4 pieces of information to connect to the database from PHP. They are Hostname, Port Number, UserName and the Password. You can find the UserName on the tab named ‘Database server’ in phpmyadmin client as shown below. By default the UserName will be “root”. In the same tab you can find the Hostname too. Whatever comes after the @ symbol in the User field of ‘Database Server’ tab will be your Hostname. By default Hostname will be “localhost”.

Hostname and Username

8. Now to find the port number you need to go back to your XAMPP control panel and get to know the port number on which MySQL is running on. By default port number will be “3306”.

9. By default, the Password will be “” (Blank). You can also change the password for a user in phpmyadmin by navigating to the ‘User Accounts’ tab. The following image shows that the “root” user has no password by default. You can click on Edit button to change the password.

User Accounts

10. Now we have all the required information to connect to a database. In our PHP script we use mysqli_connect() function to open a new connection to the MySQL server.

Syntax :

So we will be calling mysqli_connect(“localhost:3306″ ,”root”, “” , “androidbash_movies2017_db”) from our PHP script.

11. Create a new file named movies.php and paste the following code in it. Now you need to place this movies.php file in the C:\xampp\htdocs folder of your system. This file helps us to connect PHP and MYSQL with an android application. For this request “localhost:3036/movies.php?id=0”, the response will be containing movies with from id 1 to id 10. And the next request will be localhost:3036/movies.php?id=10, which gives a list of movies with id from 10 to 20.

Htdocs Folder

11. If you have followed all the above steps and if your XAMPP server is running you can test whether you are able to connect to database or not, by visiting http://localhost/movies.php?id=0 from your browser. We get a JSON response as shown below.

Query Result

Part ! : Includes steps to create a new Android application

1. Create a new Android Project in Android Studio. Give it a name and select the Minimum SDK on which your app will run on. I chose API 16 : Android 4.1 (JELLY_BEAN).

2. When you are prompted to add an activity to your application choose Blank Activity and click on next button.

3. In the next step click on Finish and in few seconds your application should be loaded in Android Studio.

4. Open build.gradle(Module:App) file of your application and add the following dependencies inside dependencies section of build.gradle (Module:App) file.

5. The application requires following permissions to work. Your app might not work unless you add this permission to your AndroidManifest.xml file.

Part !!! : Includes modifications to activity_main.xml file and other XML files

1. The build.gradle (Module:app) of your android application file should look like below.

2. After adding Internet permission to the manifest : AndroidManifest.xml looks like this.

3. Open activity_main.xml file of your android application and add the following code in it. This file corresponds to a layout that is inflated from our MainActivity. We have a Relative Layout which consists of a Recycler View in it.

4. Create a new resource file and name it as card.xml. Add the following code in it. Each item in a RecyclerView will be displayed as a card. Our card consists of an ImageView to hold the movie poster and 2 Textviews to display the movie name and the genre respectively.

5. Create a new file named menu_context.xml under res->menu folder. Add the following code in it. This will be the pop up menu that will be displayed once the user clicks on any of the movie.

6. The colors.xml file in the values folder looks like this. You can change the colors to change the look of the application.

Part !V : Includes creation of new Java classes and modification to file

1. Create a new java class in your package and name it as This is a simple POJO class which represents a movie. It has the following code in it.

2. Create a new java class in your package and name it as To use the RecyclerView widget, you have to specify an adapter and a layout manager. To create an adapter, extend the RecyclerView.Adapter class. A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user. To reuse (or recycle) a view, a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset. Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensive findViewById() lookups. Now add the following code in it.

3. Open of your android application and add the following code in it. RecyclerView provides these built-in layout managers: LinearLayoutManager which shows items in a vertical or horizontal scrolling list. GridLayoutManager which shows items in a grid. StaggeredGridLayoutManager which shows items in a staggered grid. We will be using GridLayoutManager in this tutorial. We have a method getMoviesFromDB(int) which takes an integer and calls our movies.php (PHP script) located in C:\xampp\htdocs folder .We use OkHttpClient to send this request. Our PHP script takes this integer value and returns a JSON Array of data fetched from the database. If we call getMoviesFromDB(0), then the first 10 rows in the movies_table of our database will be returned in the form of JSON array. We can parse the JSON array to get data about each movie and set it to an item in the RecyclerView.

Note : Please replace the IP address in the request with your IP address. You can get to know your IP address by entering a command ipconfig in your command prompt. Example: “http://192.168.X.XX/movies.php?id=”

That’s it!. If you have followed all the steps you can now Run your application and test how it works. The following image shows how the application works when the application runs on an Emulator. This is how we integrate PHP and MYSQL with an android application.

Running Project

If you have liked our tutorial, you can help us keep this website free for all Android enthusiasts by buying our android application from Google play store. Here is the link of our Android application : Trailers Now Pro or by donating any small amount on my PayPal account. If you have followed our tutorial please do let us know by commenting below. It makes us write more and more articles on android application development. In case if you get stuck somewhere please do comment below and let us know. We usually reply within a day.

You can download the project from the below link.

Download Project

Project on Github

Get 20% off on all New HostGator Hosting plans with Coupon: SNAPPY.

Integrate PHP and MYSQL with an Android Application

13 thoughts on “Integrate PHP and MYSQL with an Android Application

  • October 28, 2016 at 5:17 pm

    My request when trying to connect to the php script in browser does not display anything, but a blank screen. I have tried all error reporting and I cannot find the error. How can I resolve this?

    • October 29, 2016 at 4:04 am

      Hello Christopher, Make sure you have started Apache server and MySQL from XAMPP control panel. Then you need to visit phpmyadmin and run the SQL query given in Step 6 of this tutorial. Then you need to create movies.php file and add the code mentioned in Step 11. Depending on your port number, username, password and databasename your mysqli_connect(“localhost:3306”, username, password, databasename) will vary. Add it accordingly in movies.php and store this php file inside htdocs folder of xampp. Now try to run http://localhost/movies.php?id=0 in your browser.

      • October 29, 2016 at 4:54 am

        Thank you, I was able to get it working. I modified the code a bit and only used your turorial as n guideline to retrieve data for my own app. The problem is I wanted to store the image in a BLOB but seems like I cannot retrieve it then? When saving it as a link it is working fine. If you know how to retrieve and display it by saving it as a BLOB please let me know.

        But anyway, thanks for a great tutorial 🙂

  • November 1, 2016 at 12:03 pm

    Hello, its worked fine for me ! I wanna a “Sinopsis ” part in the card to show a description of each movie ! Can u help me ?

    • November 2, 2016 at 3:26 am

      Hi, Glad it worked. If you want to add a synopsis, then it is better to go for a LinearLayout instead of GridLayout. You can find the usage of LinearLayout in our RecyclerView implementationtutorial. Once you are using LinearLayoutManager, you can add another TextView in your card.xml and align it accordinglingly.You need to add another column in your database table for synopsis and fetch it just like how we have fetched movie name and populate in the TextView you have added.

  • November 12, 2016 at 8:05 am

    Thanks for the tutorial, It’s working great for me. Just a question, I have added an additional column to MySQL table called “times_watched”, an int type where I want the number to be increased whenever the corresponding movie in the same row is “booked” in the android app. May I know how to achieve that?

  • January 4, 2017 at 4:01 pm

    Thank you for such a detailed tutorial! I wanted to know if there’s a way of getting a notification in the admin panel whenever someone books a movie. According to this tutorial when a person books a movie, all he gets is a toast message, but, I want the admin to get notified whenever a user books a movie in a manner in which I have his user ID (assuming the user is logged in through firebase backed email login) and which movie that user has selected.

  • January 20, 2017 at 5:14 am

    Hello, thanks for the amazing tutorial. It works perfectly for me. I’m wondering how this will work if my mobile is connected to a different network than the the one the server is connected to. How to make this work over the internet?

    • January 27, 2017 at 3:01 am

      Thank You Naseera. And yes this works over the Internet if you can store the database and connect to it via PHP code given in the tutorial. If you have your website hosted over the internet, then you can create, store and manage databases easily using cpanel and phpmyadmin. You can store the php files you have created in the public_html folder of your website using File managers provided in the cpanel of your website. I use Hostgator to host my website and they provide easy, affordable and convenient way to manage websites.

  • February 25, 2017 at 2:14 pm

    Thank you very much, really helpful and simple tutorial. I will try to do it.

  • March 7, 2017 at 3:46 am

    It’s really helpful tutorial. However, i’m having problem with RecyclerView, where codes with “R.” is error. How to solve this problem? Thank you

  • March 25, 2017 at 11:45 am

    This tutorial was about connecting android app with the Mysql on localhost, If i want to connect my android app with mysql database placed on an online server. How it would be done ?

    • March 26, 2017 at 6:00 am

      yes this works over the Internet if you can store the database and connect to it via PHP code given in the tutorial. If you have your website hosted over the internet, then you can create, store and manage databases easily using cpanel and phpmyadmin. You can store the php files you have created in the public_html folder of your website using File managers provided in the cpanel of your website. I use Hostgator to host my website and they provide easy, affordable and convenient way to manage websites.


Leave a Reply

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