Help with clause Where in MySQL and PHP for android

1

I'm making an Android application and I need to make a query in sql using php but I do not know why it does not return anything when I ask. This is a java code for Android:

    AsyncHttpClient client = new AsyncHttpClient();
    final String url="http://pear-web.com/profe.php";
    RequestParams requestParams = new RequestParams();
    Aplicacion app = (Aplicacion) getApplicationContext();

    requestParams.add("login",app.getUsuario());
    client.get(url, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            if(statusCode==200)
            {
                progresDialog.dismiss();
                try {
                    JSONArray jsonArray = new JSONArray(new String(responseBody));
                    for (i=0;i<jsonArray.length();i++){

                            Name.add(jsonArray.getJSONObject(i).getString("Login"));
                            System.out.println(Name);
                            Apellido.add(jsonArray.getJSONObject(i).getString("apellido"));
                            Correo_profe.add(jsonArray.getJSONObject(i).getString("Correo_padre"));
                            ID.add(jsonArray.getJSONObject(i).getString("ID_profe"));


                    }
                    listView.setAdapter(new ImagenAdapter(getApplicationContext()));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }'

And this is the php code:

<?php
$hostname_localhost="";
$database_localhost="";
$username_localhost="";
$password_localhost="";
$login = $_REQUEST["login"];
$localhost=mysql_connect($hostname_localhost,$username_localhost,
$password_local    host)or die ("cannot conect");
mysql_select_db($database_localhost) or die("cannot selet DB");
$sql="SELECT Login,apellido,Correo_padre,ID_profe FROM usuario
WHERE ID_profe=$login ";  <---este es el where que menciono
$result = mysql_query($sql,$localhost);
$json = array();
if(mysql_num_rows($result)){
   while ($row=mysql_fetch_object($result)) {
       $json[]=$row;
    }
}
mysql_close($localhost);
echo json_encode($json);
?>

It should be mentioned that when I do not put the Where it shows me the query, but I want to filter the list when necessary. I already tried everything and I do not know what I'm doing wrong.

    
asked by juan 15.11.2016 в 04:00
source

3 answers

1

Definitely the problem is that the value of $login is not represented correctly within the Query, if you add it as you describe in your question, the value of $login is simply a string that is added to the Query:

$sql="SELECT Login,apellido,Correo_padre,ID_profe FROM usuario
WHERE ID_profe=$login ";

You must add single quotes so that the value of the variable $login can be represented correctly in the query:

$sql="SELECT Login,apellido,Correo_padre,ID_profe FROM usuario
WHERE ID_profe='$login'";

This is an example of a Query:

$username = $_GET['username'];
$password = $_GET['password'];
$result = mysqli_query($con,"SELECT * FROM TABLA_USUARIOS where usuario='$username' 
          and password='$password'");

By putting single quotes '$username' and '$password' , the value of these variables will be represented correctly in the Query.

    
answered by 15.11.2016 в 17:00
0

Add single quotes '' to the variables you use within a query:

 $sql="SELECT Login,apellido,Correo_padre,ID_profe FROM usuario
    WHERE ID_profe='$login'";
    
answered by 15.11.2016 в 09:57
0

Have you tried to see if you take well the value of the variables when collecting them before making the query?

$login = $_REQUEST["login"];
echo $login;
    
answered by 15.11.2016 в 09:16