Android Tutorial Connect Android to MySQL Database Tutorial

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

  1. anaylor01

    anaylor01 Well-Known Member Licensed User

    The php and the whole site which is only a very small index.html page that says hey I am new to web programming is all 0777. When I manually type in http://imgsinc.onlinewebshop.net/query.php in the address bar it gives me
    "The page you are looking for is either not existing or cannot be displayed! "
    I would assume since my index.html file is working and the query.php file is in the same folder with the same permissions that it should work. I attached the php file as a screenshot in my last post. Any suggestions on where to look?
     
  2. anaylor01

    anaylor01 Well-Known Member Licensed User

    Disregard. I am a moron.
     
  3. dmpotter

    dmpotter Member Licensed User

    Wow, Thank you.. This is a good reference.
     
  4. Helihead

    Helihead Member Licensed User

    sqlite Update data

    Hi,

    I'm trying to do an Update on a blob field in one of my tables to replace the default bmp with a different bmp in the field 'photo' in an existing record. I have looked the web over and can't find an Update example for B4A.

    The code below shows what I am trying but I get various exceptions with different variants of the Update. Usually I get an invalid token or an exception saying problem with Value.

    My Selects and Inserts work fine. Anybody see what I am doing wrong with Update?

    The db is initialized as sqlCon already by the time it gets to this code. fpath and fname are passed into this and I have confirmed that the sql statement looks correct with the right name, path and search parameters but it is failing on the syntax. I also tried various query and nonquery options but no luck.

    Thanks Jeff


    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(fpath, fname)
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    Dim Buffer() As Byte
    Buffer = OutputStream1.ToBytesArray
    sqlCon.ExecNonQuery2("UPDATE table SET VALUES('photo',?) WHERE rname = '" & name & "' AND manufacturer = '" & manu & "'",Array As Object(Buffer))
     
  5. Harris

    Harris Well-Known Member Licensed User

    sqlCon.ExecNonQuery2("UPDATE table SET VALUES(photo,?) WHERE rname = '" & name & "' AND manufacturer = '" & manu & "'",Array As Object(Buffer))




    C = SQL1.ExecQuery("SELECT * FROM "& TB &" WHERE ID = (Select MAX("& Cn &") FROM "& TB &")")

    This select statement uses the params TB and Cn. I use a pair of double quotes to start and end each section of the statement. Yours appears to have single quotes in it. Is this required in PHP?

    "SELECT * FROM "
    & TB &
    " WHERE ID = (Select MAX("
    & Cn &
    ") FROM "
    & TB &
    ")")
     
  6. Helihead

    Helihead Member Licensed User

    This select statement uses the params TB and Cn. I use a pair of double quotes to start and end each section of the statement. Yours appears to have single quotes in it. Is this required in PHP?


    Mine is not PHP just B4A to sqlite. I posted here because that's where I grabbed the original code to insert the blob that I am now trying to update. Just noticed that there was a PHP discussion going on. Sorry.
     
  7. kickaha

    kickaha Well-Known Member Licensed User

    Is that valid syntax for an update?

    I thought the syntax would be

     
  8. Harris

    Harris Well-Known Member Licensed User

    sqlCon.ExecNonQuery2("UPDATE table SET photo = ? WHERE rname = '" & name & "' AND manufacturer = '" & manu & "'",Array As Object(Buffer))

    photo = ? seems to be the correct syntax


    The select statement I supplied works fine for me. My update statements are formated in the same fashion using params.

    Hope you work it out.
     
  9. osamaguc

    osamaguc New Member

    please i'm suffering :)!! i really need to know how to set up this external database ! esp the architecture of the whole picture ! where do i have the database ? and where should i write that php ?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You should create the database in your server. Usually it can be done from the control panel. The php file should be hosted there as well.
     
  11. jukav

    jukav New Member Licensed User

    Bugs.

    Hello.

    Congratulations on your development environment you just bought.

    Go ahead my apologies for using the google translator to post in English.

    The point is that I'm trying to follow his example to connect to a mysql db, but I'm stuck on the error

    <b>Parse error</b>: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in <b>/home/s10ea9bf/public_html/conect.php</b> on line <b>22</b><br />

    [​IMG]

    [​IMG]

    I would appreciate your help.

    Greetings.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Something is wrong in your PHP script. Can you post the actual code of line 9?
     
  13. jukav

    jukav New Member Licensed User

    It is the image, ie the countries.php from their example.

    In both Dreamweaver and Eclipse says error, first in this line (single quotes), then in line 13, also by quotation marks, and finally at 21, suggesting that changes a '],' but it still fails .

    I've also tried to put <?php opening the file, but it always breaks, according to your ide, the general array ...
     
  14. jukav

    jukav New Member Licensed User

    Sorry, but I'm trying your example on my remote server, and I just read that in the emulator if you are only going local ... That's right.
     
  15. jukav

    jukav New Member Licensed User

    A thousand pardons ... had not closed the quotes after the pass. Works perfectly.

    Salu2.
     
  16. MaxApps

    MaxApps Active Member Licensed User

    http://myweb.dk/phpfile.php+TAG+VALUE how?

    Is it possible to call a php file and include a "Tag" and a "Value"



    Here is the PHP:


    $tag =trim($_POST["tag"]);
    $value =trim($_POST["value"]);

    $sql = "INSERT INTO `mdbuser1213913`.$tag (tekst) VALUES ('$value')";

    $result = mysql_query($sql);


    Kind regards

    Jmich
     
    Last edited: Aug 16, 2011
    syefri maulana likes this.
  17. Erel

    Erel Administrator Staff Member Licensed User

    Yes. The PHP script in the first post expects to get the full query and execute it. You can however instead modify the script and take the POST parameters as needed.
    You can use HttpUtils to send the POST data easily.
     
  18. danvica

    danvica Member Licensed User

    Thanks Erel for the helpfull tutorials.

    I a very newbie to the Android and webserver world but thanks to you in a couple of days I already setup a small web-db application that allows to share data on several tablet...

    Just a question. I used your PHP script to execute both "SELECT" and "INSERT" mysql statements.

    For "SELECT" no problem, mysql_fetch_assoc can manage the returned resource, but mysql_query return TRUE/FALSE if the query contains "INSERT".

    This way mysql_fetch_assoc returns a warning (not an error) saying "mysql_fetch_assoc() expects parameter 1 to be resource". And it is right !

    Is there anyone who know how to differentiate a resource from a boolean ? I tried on several PHP forum but it seems they create the queries inside the script, so they already know if a query is a "SELECT" or an "INSERT" and they know what to expect from it.

    Thanks.

    P.S. As a newbie a suggest the use of EasyPHP. It combine PHP + Apache + phpmyadmin + mysql. Great tool.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    First you should understand the security issues with allowing non-select queries.

    The simplest way I can think of is to add a GET parameter to the URL.
    Like: http : //www.example.com/script.php?insert=true
    Then in your PHP query you can get the value of 'insert' with _GET["insert"].
     
  20. danvica

    danvica Member Licensed User

    Thanks ! I'll try it.
     
Loading...