Practice English Speaking&Listening with: Best Android Studio: How to Connect Android with PHP, MySQL

Difficulty: 0

Hello, my name is Oum Saokosal.

Today I'm gonna talk about how to connect Android with PHP and MySql.

You should have Android Studio, Java, Apache Server, GenyMotion installed in your machine.

This is the prerequisite of this course.

And I will explain how Android works

So... The Android doesn't work directly with a Database like MySQL itself.

It needs a lot of connections like...

First, you have to connect from Android to PHP, but there is no a direct way from Android to PHP

You have to use the AsyncTask class. It is an API class in the Android

And in the AsyncTask class, it calls the HttpPost class, and then we can send the data from here to PHP

And the PHP does the rest.

It also does the same thing when you want to read the data from PHP or from MySQL

You have to convert the data from MySQL to JSON format/file

Or maybe XML file but many prefers JSON.

And then you use the HttpGet to get the data from here and use the AsyncTask to get the data. So...

It is very complicated.

But it is the way in Android. But I just I don't like the way, and I decided to create a class myself.

So the class is in and you can grab the code here.

This is my code and you should see this by going to the URL.

And then you should download it

But because I already downloaded it, just open it and extract it

And you download this file "GenAsync.jar". If you wanna the source code, you see it here.

And you can see the explanation here or maybe you go to my blog

And then click on this one.

And you will see an example here. So in this video, we use the some code in this blog. So...

You should have this blog open. It is better to use it along the way.

First, we have to create a database and a PHP file before we can start Android.

So, let's go to localhost > PhpMyAdmin

In this blog, I used a database called db_client

because I already have db_client but it's not working now so I have to change to db_client2 instead.

You should choose a collation of utf8_general_ci for Unicode support.

you need a table.

So... you can just come to this blog and grab the code and paste it and it'd work.

Let's try if you want.

and go to SQL tab and paste it.

Click "Go"

And then you'd have the table created for you.

And then you can insert some sample data from here

Go to SQL tab again and then "Go"

And then now you have the data

You have abc, test, kosalgeek, and password like this

So it is faster if you have the blog opened and run the code.

And then you need the PHP file to read the data from MySQL

So.. again, you can just grab the code here.

So, you go to XAMPP

And then I have a client folder already so I have to change to something else, e.g. client2.

On Mac, you have to give a permission if you want to use in here (htdocs)

So now... I have to call it "client"

And on Mac, again, you have to give permission. So, right click -> "Get Info", and then...

click the "Read" and change it the "Read and Write"

And then open the key, and choose "Apply to enclosed items"

So... it's done.

You have a full permission to read and write in this folder.

Right click, in the dialog, create a connection.php here.

(Embrace the noise!)

I'm sorry (for the noise)

You need this code for the connection.php

the database now is 2, just change to db_client2.

And then... you need another file called login.php. So...

For this login.php, I will write it because of a lot of code. Maybe I wanna explain it a little bit.

If you want some explanation about this, let me do this. So...

If you are sure what the code is... so...

this one is the server name, the database server name.

Usually it's just localhost

And the username, because I didn't change anything, it must be "root".

And the password is blank for default.

and the database here is just the database that we just created.

I use mysqli, so the code is like this.

and we connect it like this

And in here, I create a file called login.php.

First we have to have the HTML file like this.

It is very simple...

If you want to see, just go to http://localhost (in the browser)


just go to login.php

this is the simple code. OK?

it is just a textbox, a password box

a button.

There's nothing fancy here.

When you click, you will call the "action". It this code will call to itself.

Here you can write the PHP code.

So... the PHP code here.

first, you need to include the connection.php

And then because we sent a data here from a textbox.

And you... you have to have a name

the name is txtUsername. Because I want to remember it. This is my habit.

If it is a button, I usually put "btn" in the front.

It is easier to check if it is a button or text like that. OK?

The isset() is just like it is set already or what? OK.

If we sent the data through this, we can continue our task.

if not, it's gonna skip the if block.

And then, we want to change(assign) to the POST data to a variable like this.

And then you need a query.

It is just like you select the data from it (database).

If it is like, say, a username abc, just like in our database or something like this.

and a password 123. If it is correct, it should have the data and if it is not, then there is no data.

to test it, it's very simple.

first you want to use mysqli_query()

And you check by a reference, like a property called num_rows. If the rows equals 0 or less than 0, it means there is no data, record.

If more than 0, or just 1, it means there is some data.

if there is some data, then we just echo "success"

let's try... for the rest, I will explain it later.

username: abc, password: 123

so now it is success

let's say something else (absaadwa), nothing is showing. OK?

For here on, we can use it in PHP.

but our original purpose, it is just PHP but we want to use it from Android. right?

So there is another catch for this.

I will explain it after we have done something in Android. And I will come back to this. But um... let's just finish this. You know, like if and else thing.

like if login is successful, you show "success". If login is failed, you show "login failed".

Now test it again.

there is the login failed.

if username: abc and password: 123, then success.

Now you go to Android.

So in the Android, you should have the blank activity like this.

Delete the Hello World text.

I recommend you to use LinearLayout because It is simpler. It's not gonna make your user interface changed improperly.

we need only a blank text

and then a password.

but I change to the orientation of the Layout to vertical.

Oop... something's error here. Sorry. again.

then we need a button.

So I change to login and btnLogin as the id.

this one just username

but because it is et, EditText, I call it etUsername. OK?


you can give a hint.

Your username.

And this one, Your Password.

Something's error here, there is a redline.

what is it?

(this error occurred when I changed from RelativeLayout to LinearLayout before. And it didn't change the closed markup of LinearLayout)

So... now we go to Java, and I don't need this one (code). Just delete it. OK?

In here (Android), you need my library.

Where is my library? I think it is download folder here. Yes.

So you need the GenAsync.jar. Just copy it.

And you have to switch to "Project" perspective.

And in here, you can find "app", then "libs" folder. And then you just paste it.

And then you right click on GenAsync.jar again, choose "Add as Library"

So it will add my library as a library here.

So it needs some time to sync.

You should go to build.gradle here.

And You should see: compile files("libs/GenAsync.jar")

If you don't see this line, it means there is some error. So you should see this.

If you have this, mostly you have no error at all.

But to test, there is a....

Actually, I have created 2 files, 2 classes.

The most important file is called PostResponseAsyncTask

And you go here. This is the whole code.

If you want change something, you can change it here.

So... just call the class (PostResponseAsyncTask)

If you have this suggestion, it means it works well.

you say task = new PostResponseAsyncTask(this) like this.

So I made it like a general library. so it has some other methods.

For example, like... this is the method (constructor) to call AsyncResponse

when you type "this", it should have some error. It is not an error. It is just like a suggestion.

You should have a red light ball. If the yellow one, it is not right.

You have to click on "this" to have a red light ball.

And choose the Make 'MainActivity' implement 'com.kosalgeek.asynctask.AsyncResponse'

So it will create another method for you

This method is done. It's like magic.

You just pass the URL, it will grab the data and pass it in here (processFinish method)

You have to write anything

If you go to my videos (I have another video in my youtube channel), you will see how difficulty it is. I'm sorry the video is in Khmer (Cambodian language)

If you see the video, you will understand why this new created class is very important.

Because the whole process is very complicated, and I made it simple.

OK. Let's try some code here. So...

Here, you just call the execute method.

task.execute(). So in the execute here, it is a URL.

The URL that you want to provide.

Now you want to login from here in Android, right?

You have to send the data from the username and password.

Before you can call it, you have to grab the data from the XML and pass into the Java file. OK?

First you have to declare EditText etUsername, etPassword

And the Button btnLogin;

and etUsername and you have to cast it to EditiText like this

and you have to call a method called findViewbyId and you reference it to

again, etPassword = (EditText) findViewById(

and also a button

btnLogin = (Button) findViewById(

Then, this one works but we don't it to work just yet.

Just comment it.

You should have a button clicked.



Then... you should implement the method

(Sorry, I added a wrong method setOnLongClickListener here. I should have added setOnClickListener.)

Oh... i'm sorry. setLongClick?

That's why it is not happy.

If you don't have the auto import option for you, maybe some errors like this, maybe you didn't put some configuration

You go to File > Setting (for Windows) and on Mac you go to Android Studio > Preferences

And you type "auto import" and you should check all the options here

After that, if you add a class from a different package, it will automatically import the class for you.

So now, this should go to where you want to click

When you click, you read the data, right?

In this case, we don't read but just write the data, right?

So... we have to grab the texts here from etUsername and etPassword

To do that, we have to call the HasMap. The reason for that is because we will pass it as a second parameter of this (PostResponseAsyncTask constructor)

Actually it should suggest it, but if not, let's go to my blog at to see the code

We have to use the HashMap and we put the data in here, and then...

We put it into the method (constructor) as the second parameter. OK?

where is it? Just copy and paste the code!

OK. this one? we don't need it.

and then we put the postData in here.

And then you call the execute() method. So...

Now you put the postData but where you want to send to?

You want to send to localhost/login.php, right?

(Embrace the noise again!!!)

So you should see the result back


It's a little bit complicated to explain but it's just like... you send the data from here,

and this, the word "this" means this class. And then it kinda... you send this and who will take care of the result?

And it says like, OK. "this" means this class. And then, because it implements AsyncResponse, the AsyncResponse has a method called processFinish.

It means after you finish grabbing result from the URL, then it will send the result back to this processFinish method.

So this is the data. The small s here is the result.

You can just change it to "result".

The result is from the PHP file (HTML format) itself.

For example like, when you click Login, and then the data like username is abc and the password is 123, it will be the database, if there is the result, it will echo "success", right?

if not, it will echo "Login failed"

So... let's try to get the result. I will just use Toast.

Toast.makeText(this, result, Toast.LENGTH_LONG).show();

One more thing you have to do is...

you go to Android perspective

open manifests > AndroidManifest.xml

You add the uses permission to use the Internet.

If not, you cannot connect to the Web.

And also here is localhost. So...

There is one big problem here.

because the localhost here means the localhost of this emulator.

if you try to connect from your computer to emulator and we call the localhost,

when we send the apk to the emulator, the localhost here does not connect back. It just connects inside the emulator.

For built-in Android emulator, you say instead of localhost.

but if you use the GenyMotion like me, you change from number to

So let's test it.

it's building now. It takes some time.

let's try abc and 123.

OK. Now you could grab the data. OK?

It means you can get the word "success"

but why you get the HTML here?

Why? Because...

When you go to the PHP, as you can see, you didn't stop here!

It didn't stop

Because for the HTML and PHP,

it runs, and it runs, and it runs, from line 1 to the end of the line

This HTML will come back (with the result)

That is why...

I have to write something else

I have to POST another special variable inside here (inside Android)

I want to tell them like now my mobile is android.

you can just say anything, you know.

you can just say your name.

let's say we call it like this.

So now you have another variable here.

In side the PHP

When you get the data...

on HTML, like the usual HTML

You will let them (users) see something else.

But in Android, because you send the data from Android,

You have a special condition here...

You want the Android see only this text "success", not the others (the rest of HTML text)

This is the usual one that let the users see in HTML file.

But if you send data from Android, this (condition) will come.

for PHP, this condition doesn't come.

And you call echo again but then you exit. So...

this exit method will exit and will not go down to here (the rest of HTML code)

So let's see it again.

So now the test the code. We didn't change the Android code, did we?

Oh... we change the Android code a lititle bit. So...

And now let's say abc again and 123

Boom! Now you got only the word "success"

And if something else, OK... (the html code appears)

Now you want to take care of this also. OK? If you want.

If not, it's just like OK now i'm done.

After you grab the result, you test the result in here again.

OK now I got the result. If the result.equals("success")

Then I'd just say Login Success.... Login Successfully

else... just say "Login Failed"

abc, 123. Now login successfully

else, you login failed.

OK. So now it's done, but...

if you want to do a little bit more--like you have another activity,

You can just like.... OK, when the word is success, then you send them to another activity.

If not, just stay at the same activity and display the word "Login Failed".

You add another activity...

Right click on app > New > Activity > Blank Activity

Say, maybe SubActivity

I will put a big word here in the middle of the screen.

Welcome to My App

So if you login successfully, then you just say...

Intent in = new Intent(this, SubActivity.class);


When you do this, you will open another Activity.

OK now let's say you type it wrong, Login Failed.

let's say abc, 123 (the correct username and password), the boom, now you got another Activity opened.

So this is the real app, you know, like the real world app

You do a little bit inside the PHP code and say OK... the mobile is blah blah blash, then you get the data here.

And inside your Android, you just test this, and if it is correct, you will send the data (to another Activity)

For now, you don't have to think how to implement AsyncTask or other things.

What you have to do is just to think about the core thing

Like... now i send the data from an Activity, then you get the data in JSON format or something like this, and you test the result. OK?

That's all. And...

If you like this video, please don't forget to subscribe my channel, comment to below(videos) and share it to your friends...

And also, I started my blog at, So please visit my website and, you know, share it to your friends.

For now thank you and I will see you in the next video.

The Description of Best Android Studio: How to Connect Android with PHP, MySQL