0

I'm creating an android project that receives data from PHP side. But the PHP code gives no result.

I wrote this php code and checked it goes to else loop and shoes no people found . But my SQL query gives output in XAMPP SQL manually.

if($_SERVER['REQUEST_METHOD']=='POST'){
    include_once "Dbconnect.php"; 
    $phone= $_POST['phn'];

    $sql="Select 
            peoples.name as pname,
            peoples.address as addr,
            peoples.phone as phone,
            peoples.nomineephone as nphone,
            peoples.idproofnumber as idproof,
            camp.campid as cid,
            camp.Managername as mgrname,
            camp.managerno as mgrphone,
            camp.name as cname,
            camp.Location as loc 
        from 
            peoples 
        inner join camp 
            on peoples.campid=camp.campid 
        where 
            peoples.phone='$phone'";

    $result = $conn->query($sql);

    if ($result->num_rows >0) {
        while($row[] = $result->fetch_assoc()) {
            $tem = $row;
            $json = json_encode($tem);
         }
    } else {
       echo 'No people data found';
    }

    echo $json;
}

i tried to echo the phone number in PHP side i got the phone number from android and the data is available in DB with that phone number. But this code not giving the data. I expect the output of the query as json in android

15
  • 2
    Have you tried to debug your code at all (maybe a var_dump($result)? Have you checked your error logs? You also need to escape your inputs better. Commented Apr 26, 2019 at 14:51
  • 5
    Warning: You are wide open for SQL injections. Use parameterized prepared statements instead of injecting completely unescaped user data directly into the query like that. Commented Apr 26, 2019 at 14:54
  • 1.First check its coming inside if($_SERVER['REQUEST_METHOD']=='POST'){ condition? 2.Make sure phone has the value 3.echo $sql and try to execute it in phpmyadmin and make sure the phone number existing in DB Commented Apr 26, 2019 at 14:55
  • 1
    change $json = json_encode($tem); to $json = json_encode($row);break; and see Commented Apr 26, 2019 at 15:00
  • 1
    Or SELECT * FROM peoples WHERE peoples.phone='+1-555-867-5309'? With a hard coded number. Just as to verify stuff is working and then build upon it one step at a time? Oh. If the phone numbers are strings, then it would be .phone LIKE 'phone-string'. Commented Apr 26, 2019 at 15:12

2 Answers 2

1

The code at the end to fetch the data and output it is probably the cause. When you get the result from fetch_assoc(), the last iteration will return false, but as you assign it to $row[] it will add false to the data.

In this code, the loop just builds up a list of rows and then json_encodes() them on the output. It also creates the empty array first to ensure it's initialised.

It also returns the data of nothing found in the JSON rather than just echoing it out.

$rows = [];
if ($result->num_rows >0) {
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
     }
} else {
   $rows = [ 'error' => 'No data found' ];
}

echo json_encode($rows);

If you are just expecting 1 row to be returned, you could use

if ($result->num_rows >0) {
    $row = $result->fetch_assoc();
} else {
   $row = [ 'error' => 'No data found' ];
}

echo json_encode($row);
Sign up to request clarification or add additional context in comments.

2 Comments

it goes into else loop only again
Can you try changing the inner join to left join in case there isn't a match in that record.
0

Maybe this will help. Basically just rewrote the last lines of what you shared. Think it is self explanatory. My gripe is you should be able to better troubleshoot the issue. Work on your debugging. Ask something specific or maybe answer your own question.

$result = $conn->query($sql);
if ($result === false) {
  die('query error');
}

$row = $result->fetch_array(MYSQLI_ASSOC);
if (empty($row)) {
  die('no results');
}

//your code suggests you expect one result. So unless phone number is a unique index in database you should handle that better here.

$jsonOut = json_encode($row);

header('Content-Type: application/json');
echo $jsonOut;

2 Comments

it gives no results as the result
i'm sure the data is there in the db because i login with the same phone number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.