65

I have a website already setup which uses mysql database. I want to know how can i connect my app to that database.

What i wanna achieve is that my app should make a request to find a table of a defined "ID" or name. The table contains links to images or image names. I want the app to retrieve those images and display them on the android app.

What are the ways this can be done? Can I use PHP to develop an android app?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Arihant
  • 3,847
  • 16
  • 55
  • 86
  • 1
    Your Android app will be written in Java. Personally, I then find it works best to then also write my server side in Java (code can be shared). – Tom Mar 31 '13 at 18:39
  • http://androidbash.com/connecting-android-app-to-a-database-using-php-and-mysql/ Here the data is being populated from backend database. – SRBhagwat Oct 25 '16 at 02:24

4 Answers4

22

Android does not support MySQL out of the box. The "normal" way to access your database would be to put a Restful server in front of it and use the HTTPS protocol to connect to the Restful front end.

Have a look at ContentProvider. It is normally used to access a local database (SQLite) but it can be used to get data from any data store.

I do recommend that you look at having a local copy of all/some of your websites data locally, that way your app will still work when the Android device hasn't got a connection. If you go down this route then a service can be used to keep the two databases in sync.

Dobbo
  • 1,188
  • 3
  • 16
  • 27
8

The one way is by using webservice, simply write a webservice method in PHP or any other language . And From your android app by using http client request and response , you can hit the web service method which will return whatever you want.

For PHP You can create a webservice like this. Assuming below we have a php file in the server. And the route of the file is yourdomain.com/api.php

if(isset($_GET['api_call'])){
    switch($_GET['api_call']){
       case 'userlogin':
           //perform your userlogin task here
       break; 
    }
}

Now you can use Volley or Retrofit to send a network request to the above PHP Script and then, actually the php script will handle the database operation.

In this case the PHP script is called a RESTful API.

You can learn all the operation at MySQL from this tutorial. Android MySQL Tutorial to Perform CRUD.

Kashfa Khan
  • 2,165
  • 1
  • 11
  • 8
Mukesh Y
  • 762
  • 6
  • 16
3

Yes you can connect your android app to your PHP to grab results from your database. Use a webservice to connect to your backend script via ASYNC task and http post requests. Check this link for more information Connecting to MySQL

kabuto178
  • 3,129
  • 3
  • 40
  • 61
2

Use android vollley, it is very fast and you can betterm manipulate requests. Send post request using Volley and receive in PHP

Basically, you will create a map with key-value params for the php request(POST/GET), the php will do the desired processing and you will return the data as JSON(json_encode()). Then you can either parse the JSON as needed or use GSON from Google to let it do the parsing.

Community
  • 1
  • 1