Search code examples
phpandroidselectgalaxy

SELECT statement called from HTTP post asynctask fails ONLY on galaxy s4 device


This time i got a really strange situation:
I have an asynctask that queries a php page to retrieve some information. Everything is working fine until i try the same application on a Samsung Galaxy s4. On every other device the query works. Only on s4 the php page returns "select users query failed" (it's my own custom error message when the select statement fails). Does anyone have suggestions?

  • I am sure that values sent from the device are correct as i can log them out before sending.
  • I'm sure that values arrives on the php page because i can dump them out on the php page.

But the SELECT statement still fails ONLY with s4. The exactly same statement with exactly the same values called from another device works.

Crazy things....

Below there is the asynctask that calls the php page:

private class AsyTask extends AsyncTask<ArrayList<String>, Void, JSONObject> {
    HttpResponse response;
    HttpEntity entity;
    InputStream instream;

    protected void onPreExecute() {
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected JSONObject doInBackground(ArrayList<String>... arg0) {

        ArrayList<String> passed = arg0[0];

        HttpClient cli_req = new DefaultHttpClient();

        HttpPost cli_post = new HttpPost(Checkup.getDB_LINK()+"cal_geteventlist.php");

        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);

            nameValuePairs.add(new BasicNameValuePair("mail", passed.get(0)));
            nameValuePairs.add(new BasicNameValuePair("dayevent", passed.get(1)));
            nameValuePairs.add(new BasicNameValuePair("day", passed.get(2)));
            nameValuePairs.add(new BasicNameValuePair("time", passed.get(3)));

            Log.d("Sending value","Mail: "+passed.get(0));
            Log.d("Sending value","dayevent: "+passed.get(1));
            Log.d("Sending value","day: "+passed.get(2));
            Log.d("Sending value","time: "+passed.get(3));

            cli_post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
            response = cli_req.execute(cli_post); //response from the PHP file

            if(response.getStatusLine().getStatusCode() == 200){

                Log.d("Async Task","answer ready");

                entity = response.getEntity();

                if(entity != null){
                    instream = entity.getContent();
                    JSONObject jsonResponse = new JSONObject(Checkup.convertStreamToString(instream));
                    return jsonResponse;
                } else {
                    return null;
                }
            }
        } catch(Exception e){
            return null;
        }
        return null;
    }
    @Override
    public void onPostExecute(JSONObject result) {

        if (result != null) {
            if (result.has("result")) {
                try {
                    if (result.getString("result").matches("success")) {
                        ArrayList<DayEvents> tList = new ArrayList<DayEvents>();
                        JSONArray jArr = result.getJSONArray("dayevents");
                        int c = 0;
                        while (c < jArr.length()) {
                            JSONObject tmp = jArr.getJSONObject(c);
                            tList.add(new DayEvents(tmp.getString("nick"),tmp.getString("email"),tmp.getString("idpos"),tmp.getString("eventtype"),tmp.getString("year"),null));
                            c++;
                        }
                        setList(tList);
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

and this is the php page that receive values as POST from the asynctask:

<?php
include 'cal_config.php';

$_mail = $_POST["mail"];
$_dayevent = $_POST["dayevent"];
$_day = $_POST["day"];
$_time = $_POST["time"];

$conn = mysqli_connect($_dbIp,$_dbUte,$_dbPass,$_dbDbase) or die("Connection failed");
$qCoordEvent = "SELECT X(coords),Y(coords) FROM event_ute WHERE dayeventute='".$_dayevent."'";
$rCoordEvent = mysqli_query($conn,$qCoordEvent) or die(exErr("Getting dayevent coords failed"));
$wCoords = mysqli_fetch_row($rCoordEvent);
$lat_event = $wCoords[0];
$lon_event = $wCoords[1];

$qLocalize = "SELECT *, ( 6371 * acos( cos( radians(loc_lat) ) * cos( radians(" . $lat_event . ") ) * cos( radians(" . $lon_event . ") - radians(loc_long) ) + sin( radians(loc_lat) ) * sin( radians(" . $lat_event . ") ) ) ) AS distance FROM ANA_EVENT HAVING distance < loc_range ORDER BY distance";
$rLocalize = mysqli_query($conn,$qLocalize) or die(exErr("Getting localized event by coords failed"));
$count = mysqli_num_rows($rLocalize);
$c = 0;

while ($tmp = mysqli_fetch_assoc($rLocalize)) {
    if ($c == ($count-1)) { $ifv = "'"; }
    else { $ifv = "', "; }
    $locString .= "'".$tmp["email"].$ifv;
    $c++;
}

$selDay = getDay($_day);

$qFinal = "SELECT idpos,email,nick,eventtype,year FROM ANA_EVENT WHERE email IN (".$locString.") AND ((".$selDay[0]."='1' AND MAKETIME(".getTime($_orario).") BETWEEN ".$selDay[1]." AND ".$selDay[2].") OR (disp_tut='1' AND MAKETIME(".getTime($_orario).") BETWEEN disp_tut_from AND disp_tut_to) OR (".$selDay[0]."='1' AND ".$selDay[1]."=".$selDay[2].") OR (disp_tut='1' AND disp_tut_from=disp_tut_to)) ORDER BY year";
$rFinal = mysqli_query($conn,$qFinal) or die(exErr("Final selection query failed")); //this is where ONLY galaxy s4 fails!
$i = 0;

while ($res = mysqli_fetch_assoc($rFinal)) {
    if ($res["email"] == $_mail) { continue; }
    $dati[$i] = $res;
    $i++;
}

$resultArray["result"] = "success";
$resultArray["dayevents"] = $dati;

echo json_encode($resultArray);


//# - FUNCTION SECTION - #
function getDay($g) {
    if ($g == "monday") { $a[0] = "disp_mon"; $a[1] = "disp_mon_from"; $a[2] = "disp_mon_to"; return $a; }
    if ($g == "tuesday") { $a[0] = "disp_tue"; $a[1] = "disp_tue_from"; $a[2] = "disp_tue_to"; return $a; }
    if ($g == "wednesday") { $a[0] = "disp_wed"; $a[1] = "disp_wed_from"; $a[2] = "disp_wed_to"; return $a; }
    if ($g == "thursday") { $a[0] = "disp_thu"; $a[1] = "disp_thu_from"; $a[2] = "disp_thu_to"; return $a; }
    if ($g == "friday") { $a[0] = "disp_fri"; $a[1] = "disp_fri_from"; $a[2] = "disp_fri_to"; return $a; }
    if ($g == "saturday") { $a[0] = "disp_sat"; $a[1] = "disp_sat_from"; $a[2] = "disp_sat_to"; return $a; }
    if ($g == "sunday") { $a[0] = "disp_sun"; $a[1] = "disp_sun_from"; $a[2] = "disp_sun_to"; return $a; }
 }

 function getTime($t) {
    $exploded = explode(":",$t);
    return $exploded[0].",".$exploded[1].", 00";
 }

function exErr($errMsg) {
    $resultArray["result"] = "failed";
    $resultArray["errmsg"] = $errMsg;
    echo json_encode($resultArray);
    exit;
}
//# - FUNCTION SECTION END - #

?>

I hope that now it is clearer. Please help someway.


Solution

  • Ok finally i got the issue:

    i don't know why but here:

    function getDay($g) {
        if ($g == "monday") { $a[0] = "disp_mon"; $a[1] = "disp_mon_from"; $a[2] = "disp_mon_to"; return $a; }
        if ($g == "tuesday") { $a[0] = "disp_tue"; $a[1] = "disp_tue_from"; $a[2] = "disp_tue_to"; return $a; }
        if ($g == "wednesday") { $a[0] = "disp_wed"; $a[1] = "disp_wed_from"; $a[2] = "disp_wed_to"; return $a; }
        if ($g == "thursday") { $a[0] = "disp_thu"; $a[1] = "disp_thu_from"; $a[2] = "disp_thu_to"; return $a; }
        if ($g == "friday") { $a[0] = "disp_fri"; $a[1] = "disp_fri_from"; $a[2] = "disp_fri_to"; return $a; }
        if ($g == "saturday") { $a[0] = "disp_sat"; $a[1] = "disp_sat_from"; $a[2] = "disp_sat_to"; return $a; }
        if ($g == "sunday") { $a[0] = "disp_sun"; $a[1] = "disp_sun_from"; $a[2] = "disp_sun_to"; return $a; }
    }
    

    it seems that value "day" passed by samsung galaxy s4 care about Capitalized letters and the value "day" passed by other devices not. As i capitalize the first letters of day name in the function "getDay()" it works on galaxy s4 and doesn't work on other devices.

    So, to solve the issue i assigned every string value coming from HTTP POST request like this:

    $_day = strtolower($_POST["day"]);
    

    and now it works on every device.

    Thanks all for help, especially to @MarcB with this comment:

    you probably have a basic if (failed) { something blew up} instead of if (failed) {output actual DB error message}-type error handling

    as after i changed this:

    $rCoordEvent = mysqli_query($conn,$qCoordEvent) or die(exErr("Final selection query failed"));
    

    with this:

    $rCoordEvent = mysqli_query($conn,$qCoordEvent) or die(exErr(mysqli_error($conn)));
    

    and finally got the real error message coming from the SELECT query.

    I want to quote @greenapps too as i think it's a good point:

    Try with strcmp() instead of == operator. You are sure you dont have something like "thursday\n" ? Add an echo() and a return to getDay() if none of the if's hold. Add code to handle the returned null appropriately. –