• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

What and how is the best way to connect to a MySQL database?

8BiT

Lurker
So I'm sure I'm the millionth person to ask this question here but I just can't seem to find a definitive or decisive answer anywhere.

LONG Version:
I'm working on an app that requires lots of user to database communication to perform all of the basic CRUD operations. After extensive searching I've found multiple solutions to this problem, ranging from Volley to ORMs, to PHP scripts and about 5 confusing tutorials on how to implement the latter.

My only issue is, I can't seem to find an up to date (2018) relevant tutorial for doing this via PHP. Seems a lot of the tutorials use deprecated methods and/or don't provide a full explanation for what is going on. For a beginner in this area, this can be quite daunting to figure out the right way of doing this.

SHORT version:
So my question is, what is the best way to go about connecting an Android app to a MySQL database, and if it's via PHP, then can someone point me in the right direction for doing it right and proper?
 
Yes I've answered this one a couple of times recently ;)
There are several ways to implement it. You can implement a REST web service, but this requires using a servlet container, for example Tomcat.
The easiest solution is by using the web server (Apache) to receive the web request, and invoke a PHP script.
At the risk of repeating what you've already investigated, there are several tutorials on this subject, and you first need to become familiar with how PHP scripts are constructed. Do you have that knowledge?
I found a tutorial which could be useful to you here

https://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

It would maybe help to learn what your current area of confusion is, so we can advise on that particular aspect. As there are several moving parts to this problem, it would help to be more focussed, and possibly explain what you've already tried.

If the above tutorial doesn't fulfil your needs, we'll have to look at something else to demystify this for you.
 
Yes I've answered this one a couple of times recently ;)
...
The easiest solution is by using the web server (Apache) to receive the web request, and invoke a PHP script.
At the risk of repeating what you've already investigated, there are several tutorials on this subject, and you first need to become familiar with how PHP scripts are constructed. Do you have that knowledge?
...
It would maybe help to learn what your current area of confusion is, so we can advise on that particular aspect. As there are several moving parts to this problem, it would help to be more focussed, and possibly explain what you've already tried.

Thanks for the reply. I'll try and clarify/focus my situation as much as possible.

My knowledge:
Android Studio (i.e. knowledge of Android specific classes & project structure): Beginner (i.e. learning as I go)
Java: Intermediate
SQL: Intermediate (i.e.enough for what's needed for this)
PHP: Beginner (i.e. Iearning as I go. Although knowing Java relatively well is coming in handy as I only have to learn syntax and PHP specific classes/functions)

Thanks for the link, I have indeed already seen it and given it a go, although that particular tutorial ended up only partly working.

I have also tried a tutorial that used Volley which ended up working just fine, but the tutorial only went through creating and retrieving records, no help on updating or deleting records. Couldn't find as much help with volley as I would've hoped for.

I am currently attempting to follow THIS tutorial to see how it goes. My best current understanding is that the Android app would use an API (which I'd build with PHP) which could access the DB and perform all the operations.

Honestly my only problem lies in trying to understand how it all connects together. I have not found any tuts explaining which classes in Android Studio do exactly what and how they function, most people seem to just be giving out the code with little to no explanation to how it works. (i.e. I can easily copy paste code, but that doesn't help me understand the inner workings of it all). The same goes for the many PHP scripts I've seen, most tuts just say "just write this and it should work", or something similar. Also, the fact that HttpClient has been deprecated and all the new classes all have such similar sounding names, makes it difficult to understand what's doing what as well.

LONG STORY SHORT, I was hoping somebody had put together a nice tutorial explaining each step and perhaps some diagrams to help understand it all. That's what I'm having an issue with really, understanding it all, and I was hoping someone had a link to such a wonderful resource.
 
I understand your confusion. Web programming is something that has come a long way, and the underpinning technology behind it is HTTP. I applaud your efforts in trying to understand what's going on. Most people simply paste code into their app, without really knowing how it works.
Unfortunately the very nature of web evolution means that there are an absolute plethora of add-on technologies which are intended to make it all easier for the programmer to create and manage web services. A side effect of this is that it becomes more difficult to understand what's really going on under the covers. In practice, this means you often have to draw information from multiple resources. There may not be a wonderful resource which explains the whole thing from your point of view.
However that tutorial you linked to looks very promising, and seems to explain things from both client and server side.
 
I understand your confusion. Web programming is something that has come a long way, and the underpinning technology behind it is HTTP. I applaud your efforts in trying to understand what's going on. Most people simply paste code into their app, without really knowing how it works.
Unfortunately the very nature of web evolution means that there are an absolute plethora of add-on technologies which are intended to make it all easier for the programmer to create and manage web services. A side effect of this is that it becomes more difficult to understand what's really going on under the covers. In practice, this means you often have to draw information from multiple resources. There may not be a wonderful resource which explains the whole thing from your point of view.
However that tutorial you linked to looks very promising, and seems to explain things from both client and server side.

Thanks again for the reply. I've just finished going through that last tutorial and it seems to work great. Does everything I need it to. Fully understand the PHP/API side of things and the server side of things. The only thing slowing me down now is all the Android/Java code, just a matter of grinding through it and reading up on every class and method now. It's gonna be a loooong night. :specsdroid:

But thanks for the insight into the web side of android.:thumbsupdroid: Looks like it's gonna take some time to become proficient at this.
 
Well there's a lot of boiler plate code involved in this, and you can see why libraries like Volley appeared, to abstract all this away from the programmer, and present a cleaner and more concise interface. But under the covers, all that code is what Volley is doing. Still good to know about, but a lot of people these days do use libraries such as Volley to simplify things.
 
Sorry to bring up an old thread. But, if you're familiar with PHP, then that's a good start.

I'd look into OkHTTP. It's really easy to use, and I've used it quite a lot. Saves a lot of time, and it's better than writing your own http code from scratch.

In your PHP, you can use
PHP:
$_POST['whatever']
and
PHP:
$_GET['whatever']

If you want to retrieve any data from a MySQL database, write your PHP file that connects to the database. Depending on what you want, you can use get and post (example above). OkHttp allows you to easily to send requests along with data, and returns JSON.
 
Last edited:
if you want to fetching data from MYSQL database write php script and convert json data read this article

Hi and welcome to Android Forums. While we don't disallow links to external blog sites, we ask that the significant content of the blog article be included on this forum, rather than just a plain link. You are welcome to contribute to discussions here, but we don't really allow members to sign up, for the purposes of spamming the forum with blog links. Just something to be aware of :thumbsupdroid:
 
Back
Top Bottom