Android Tutorial Connect Android to MySQL Database Tutorial

Discussion in 'Tutorials & Examples' started by Erel, Mar 21, 2011.

  1. swissmade

    swissmade Active Member Licensed User

    Don't get this working

    I have a MYSQL Database where I like to get some info from.
    When I copy the php source to the webroot and connect with the app.
    Then I get in the LogCat the source of the php file.:BangHead:
    I have a connection to the database when I debug the php Script.
    What i'm doing wrong here:sign0085:
    Server is Xampp.
    Last edited: May 8, 2011
  2. Erel

    Erel Administrator Staff Member Licensed User

    Your server should run the PHP script. For some reason it now treats it as a text file.
  3. swissmade

    swissmade Active Member Licensed User


    Thanks again for your fast respond.
    Found the error the Script started with <? and not with <?php

  4. Reemj

    Reemj Member Licensed User


    maybe this question is not suitable

    but I create DB and PHP file

    this error is appear

    Compiling code. Error
    Error parsing program.
    Error description: Unknown type: httpclient
    Are you missing a library reference?
    Occurred on line: 2
    Dim hc As HttpClient

    what is mistake??
    I need to answer
  5. Erel

    Erel Administrator Staff Member Licensed User

    You should add a reference to the Http library. It is done in the Libraries tab in the right pane. Note that it can only be done with the full version.
  6. Hans Scholten

    Hans Scholten Member Licensed User

    Is there someone in this group who can change this part of php code in asp code

    $query = file_get_contents("php://input");
    $sth = mysql_query($query);

    if (mysql_errno()) {
    header("HTTP/1.1 500 Internal Server Error");
    echo $query.'\n';
    echo mysql_error();
    $rows = array();
    while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;
    print json_encode($rows);
  7. Reemj

    Reemj Member Licensed User

    if I change the line 40 to

    req.InitializePost2("C:\xampp\htdocs\countries.php", query.GetBytes("UTF8"))

    this error appears

    An error has occured in sub: main excuteremotequery
    (B4A line:40)
    req.InitializePost2("C:\xampp\htdocs\countries.php" , query.GetBytes("UTF8"))
    Illegal character in schemeSpecificPart at index 2:

    Thanks a lot
  8. Reemj

    Reemj Member Licensed User

    I edit php file to this

    $databasehost = "localhost";
    $databasename = "android";
    $databaseusername ="root";
    $databasepassword = "1234";

    the type of ID is VARCHAR (5)
    population is INT (5)
    name VARCHAR (20)

    Last edited: May 16, 2011
  9. Erel

    Erel Administrator Staff Member Licensed User

    The PHP script should be located in a server that can execute the script. It cannot be accessed as a regular file.
  10. Reemj

    Reemj Member Licensed User

    thank you

    thank you ,

    I changed the link to this http://localhost/countries.php

    and change type of populaion from VARCHAR to text


    another problem appears,

    in java.lang.exception: sub
    hc_responceerror signture dose not match expected signuter.


    tahnks for the help
    Last edited: May 17, 2011
  11. Erel

    Erel Administrator Staff Member Licensed User

    You should update your Http library. There is a link at the bottom of the first post.
  12. Reemj

    Reemj Member Licensed User

    tank you fr answer me,

    after adding bridge the mobaile can not connect to MYSQ.

    MYSQL on laptop ..

    I think because IP

    the IP: http://xx.x.x.x/countries.php

    thanks a lot :)
  13. Reemj

    Reemj Member Licensed User

    when i bridge connect between a laptop and a mobile it brings only interface.
    how can i bring both the interface and the data on databae?

  14. Erel

    Erel Administrator Staff Member Licensed User

    I'm sorry but I don't understand the issue here. Are you accessing a remote MySQL server?
  15. Reemj

    Reemj Member Licensed User

    yes, with B4A bridg

    I can't access to database.

    but if the application work on emulator it is work.
  16. Erel

    Erel Administrator Staff Member Licensed User

    As I wrote as a response to your question in a different thread, B4A Bridge only launches the program. If you have any problem while running the code it is not related to B4A Bridge.
  17. rajaramcomputers

    rajaramcomputers Member Licensed User

    MySQL and Basic4Android

    I tried the example provided by hosting the service in my site

    Let us assume if someone not hosted their site anywhere and want to test with a (eg. Xampp Server) local server like http://localhost/countires.php with the emulator - any suggestion to try it with basic4Android.:sign0085:
  18. Erel

    Erel Administrator Staff Member Licensed User

    You can test it with our server. The php script should be working.
  19. anaylor01

    anaylor01 Well-Known Member Licensed User

    I need some help with this. Your example runs just fine. I tried to alter it to match a DB I created on my webserver but I get a 404 error. Here is what I did. I copied the attached query.php script to the same folder as my index.html. I then altered your example to match my DB.
    Sub Process_Globals
    Dim hc As HttpClient
    End Sub

    Sub Globals
    Type TwoLines (First As String, Second As String)
    Dim lblPopulation As Label
    Dim ListView1 As ListView
    Dim lblCountry As Label
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    End If
    End Sub
    Sub FetchCountriesList
    '  ProgressDialogShow("Fetching list of countries")
        'Gets all the available countries
        ExecuteRemoteQuery("SELECT col1, col2 FROM table1 ORDER BY col1", COUNTRIES_LIST)
    End Sub
    Sub ListView1_ItemClick (Position As Int, Value As Object)
    If IsBackgroundTaskRunning(hc, COUNTRY_POPULATION) Then
    ToastMessageShow("Wait for previous call to finish."False)
    End If
    Dim tl As TwoLines
        tl = Value
        lblCountry.Text = tl.Second
        lblPopulation.Text = 
    "Calling server..."
    End Sub
    Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
    Dim req As HttpRequest
    "", query.GetBytes("UTF8"))
        hc.Execute(req, TaskId)
    End Sub
    Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log("Error: " & Reason & ", StatusCode: " & StatusCode)
    If Response <> Null Then
    End If
    End Sub
    Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim res As String
        res = Response.GetString(
    Log("Response from server: " & res)
    Dim parser As JSONParser
    Select TaskId
    'add the countries to the ListView
                Dim countries As List
                countries = parser.NextArray 
    'returns a list with maps
                For i = 0 To countries.Size - 1
    Dim m As Map
                    m = countries.Get(i)
    'We are using a custom type named TwoLines (declared in Sub Globals).
                    'It allows us to later get the two values when the user presses on an item.
                    Dim tl As TwoLines
                    tl.First = m.Get(
                    tl.Second = m.Get(
                    ListView1.AddTwoLines2(tl.First, tl.Second, tl)
    Dim l As List
                l = parser.NextArray
    If l.Size = 0 Then
                    lblPopulation.Text = 
    Dim m As Map
                    m = l.Get(
                    lblPopulation.Text = m.Get(
    End If
    End Select
    End Sub

    Attached Files:

  20. Erel

    Erel Administrator Staff Member Licensed User

    Http 404 error means that the page cannot be found. Your php file is probably not accessible or the address is wrong.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice