Android Question Insert into remote MySQL problem [SOLVED]

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:

<?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();
}
else
{
$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.
 

igscomp

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.
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.
 

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.
 

igscomp

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

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

And int B4A i use:

If res <> "" Then
Dim parser As JSONParser
parser.Initialize(res)

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!
 
Top