android - How to create a SQL query in PHP with differents variables -
i have problem when create sql query depends on many variables user select in different checkboxes. make httprequest , them, when proposed create query, check variable , create query, gradually. show php code:
$link = mysql_connect($hostname, $username, $password) or die('no se pudo conectar: ' . mysql_error()); //echo 'conectado satisfactoriamente'; mysql_select_db('agenda juvenil') or die('no se pudo seleccionar la base de datos'); mysql_query('set character set utf8'); $query="select id, title, barrio_smultiple, coordenadas_p_0_coordinate, coordenadas_p_1_coordinate, gratuita_b eventosdiarios where"; // check post data if (isset($_get['franjas0'])){ $franja0 = $_get['franjas0']; $query.="franja_smultiple in ('$franja0'"; } if (isset($_get['franjas1'])){ $franja1 = $_get['franjas1']; $query.=",'$franja1'"; } if (isset($_get['franjas2'])){ $franja2 = $_get['franjas2']; $query.=",'$franja2'"; } $query.=")"; // product products table $result = mysql_query($query) or die('consulta fallida: ' . mysql_error()); if (mysql_num_rows($result) > 0) { // looping through results // products node $response["eventos"] = array(); while ($row = mysql_fetch_array($result)) { // temp user array $evento = array(); $evento["id"] = $row["id"]; $evento["title"] = $row["title"]; $evento["barrio_smultiple"] = $row["barrio_smultiple"]; $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"]; $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"]; $evento["gratuita_b"] = $row["gratuita_b"]; // push single product final response array array_push($response["eventos"], $evento); } // success $response["success"] = 1; // echoing json response echo json_encode($response); } else { // no products found $response["success"] = 0; $response["message"] = "no se han encontrado eventos"; // echo no users json echo json_encode($response); }
the query must return something, have nothing. variables sent throught
list<namevaluepair> params = new arraylist<namevaluepair>();
i using android. can me please??
thanks in advance.
regards.
your code needs have space after where. needs say
...b eventosdiarios ";
rather than
....b eventosdiarios where";
or you'll illformed query.
Comments
Post a Comment