Skip to content Skip to sidebar Skip to footer

Get Max Id And Min Id From Specific Column In Mysql Php

I'm new to php and now trying to retrieve the data from MySQL to android. This is my work_details table In RetrieveTotalHours function, I want to retrieve the min id time-in and

Solution 1:

replace this section:

$result=array();
while($row=mysqli_fetch_array($res)){
    array_push($result,array('id'=>$row[0],'timeIn'=>$row[1],'timeOut'=>$row[2]));
}
echo (json_encode(array("result"=>$result)));

with:

echo json_encode($res);

you create a lot of arrays, when you make push in while loop you push array in $result array, then when you echo you put all results in another array. and this is more complex.

Solution 2:

@John,

Check my answers about this errors:


Here us the issue:

echo json_encode($res);

You are encoding the $res variable.

$res = mysqli_query($con,$sql);

mysqli_query() returns a mysqli_result object.

It is the resultset, you cannot encode them into JSON.

You need to fetch it to a PHP array first like you did in the loop.

You did not use that array for the JSON encoding so echo json_encode($res); is not correct it should have been echo json_encode($result);

You can also simplify this code:

$res = mysqli_query($con,$sql);
$data=array();
while($row=mysqli_fetch_array($res)){
    $data[] = $row;
}
echo json_encode($data);

This error is because you are trying to parse it as a JSONObject:

JSONObject jsonObject = newJSONObject(json);

It should be a json array:

JSONArray jsonArray = newJSONArray(json);

Post a Comment for "Get Max Id And Min Id From Specific Column In Mysql Php"