Android Question Insert into remote MySQL problem [SOLVED]

Discussion in 'Android Questions' started by igscomp, Jan 28, 2015.

  1. igscomp

    igscomp Member Licensed User

    Hi all, when i try to do this:

    EjecutaConsultaRemota("insert into FuncionDiaria (IdFuncionDiaria, Fecha, IdOperario, IdFuncion) values (" & Registro.Get("idfunciondiaria") & ", '" & Registro.Get("fecha") & "'," & Registro.Get("idoperario") & "," & Registro.Get("idfuncion") & ")" , SUBIR_FUNCION)

    Sub EjecutaConsultaRemota(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("http://???/conexion_android.php", Query)

    Log("Sentencia SQL: " & Query)
    End Sub

    I obtain this Warning:

    Response from server: <br />
    <b>Warning</b>: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in <b>/home/.../conexion_android.php</b> on line <b>22</b><br />

    And in sub JobDone(Job As HttpJob) i obtain an error and app dies:
    java.lang.RuntimeException: JSON Array expected.

    This is conexion_android.php:


    $databasehost = "localhost";
    $databasename = "moqowpkk_???????";
    $databaseusername ="moqowpkk_????";
    $databasepassword = "???????";

    $con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
    mysql_select_db($databasename) or die(mysql_error());
    mysql_query("SET CHARACTER SET utf8");
    $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)) { --> THIS IS LINE 22
    $rows[] = $r;
    print json_encode($rows);

    I don'w know how to solve this problem, can someone help me?

    Anyway record is inserted but app fails.

    If i make a Select query works fin.

    Thanks in advance.
  2. Erel

    Erel Administrator Staff Member Licensed User

    This php code works with SELECT statements. In the case of INSERT there is no result so you get the warning. You can add a parameter to the script to distinguish between queries and commands and only convert the result to json in the case of queries.
    igscomp likes this.
  3. igscomp

    igscomp Member Licensed User

    Hi Erel, thanks for the answer. I understand and i think that this is easy to do but i am not a very good PHP programmer. Can you help me with the change? I use this PHP code by another post that you send.... :(

    I don't know how to add a parameter to a PHP script and then use it by B4A.

    Thanks for your help.
  4. inakigarm

    inakigarm Well-Known Member Licensed User

  5. sorex

    sorex Expert Licensed User

    follow inakigarm's link.

    Erel's example is just an example, it's not for use into production as it ain't safe to use since you can inject queries to read out everything or trash data.
  6. igscomp

    igscomp Member Licensed User

    Hi, i have solved my problem changing PHP file in this way:

    if(strpos(strtoupper($query), "INSERT") !== false){
    print "";
    while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;
    print json_encode($rows);

    And int B4A i use:

    If res <> "" Then
    Dim parser As JSONParser

    Dim Registros As List
    Registros = parser.NextArray 'returns a list with maps
    End If

    I don't know if it is the best way but works for me!

    Thanks all!
  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