1

I have a query (mysql) in php which when encoded to json only return the last result of the query however I am sure that the query returns more than one result:

$counter=0;
$reply=array();
$result = mysqli_query($conn,$stringQuery);;
if ($result->num_rows > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $reply["json"]=$row;
        $counter++;
    }

    echo json_encode($reply);
} else {
    echo json_encode("0 results");
}
$conn->close();

when I stringify the result in javascript it only return the last entry of the table, or single result:

$.ajax({
    type: 'POST',
    url: 'SearchQuery.php',
    data: {
        'json': cond
    },
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    dataType: 'json',
    success: function(response) {
        alert(JSON.stringify(response["json"]));

    }
});
2
  • $result = mysqli_query($conn,$stringQuery);; Extra semicolon Commented May 26, 2015 at 13:51
  • fixed it but the problem remains Commented May 26, 2015 at 14:00

3 Answers 3

3

Try this:

while ($row = mysqli_fetch_assoc($result)) {
    $reply[] = $row;
    // ^^^^^^^^^^^^^

    $counter++;
}
echo json_encode($reply);
Sign up to request clarification or add additional context in comments.

3 Comments

if i do that and change the alert in javascript to: alert(JSON.stringify(response)); nothing happens, there is no alert; Somethings happens in the php, however no error is presented
@user1281678 Try alert(JSON.stringify(response)); Because you are not sending data in json now
as I said before: if i do that and change the alert in javascript to: alert(JSON.stringify(response)); nothing happens, there is no alert; Somethings happens in the php, however no error is presented
1

Here

$reply["json"]=$row; 

in this line your last fetch row in while loop overwrite existing result so it is showing only last row as a result.

You can try

$reply["json"][]=$row; 

So at ajax in success function

alert(JSON.stringify(response["json"]));

you can have a data accessible via key name "json".

1 Comment

change contentType: "application/json" in ajax call
1

Try to change fetch_assoc() with fetch_array()

while($row=$result->fetch_array()){
       $reply[] = $row;
       $counter++;
}
echo json_encode($reply);

Or try:

while($row=$result->fetch_array()){
     $items.=<<<EOD
    {
"r": "{$row['yourfield']}",
},
EOD;
$counter++;
}

header('Content-type: application/json');
?>
{
    "response": [
        <?php echo $item;?>
    ]
}

6 Comments

$counter=0; //$reply=array(); $result = mysqli_query($conn,$stringQuery); if ($result->num_rows > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { $reply = array($counter => $row); //$reply[]=$row; $counter++; } echo json_encode($reply); } else { echo json_encode("0 results"); } $conn->close(); this should work but it doesn't
where you have error??? in console.log???? Do you have changed the $row['yourfield'] with your fieldname???? Are you sure that EOD code is written correct without format???? check it because the code is correct!!! I use it in my function page.
sorry my mistake. I've tried it now and no error and in the alert() gives a response[], empty
this is the code I have put together with your else{ $counter=0; //$reply=array(); $result = mysqli_query($conn,$stringQuery); if ($result->num_rows > 0) { // output data of each row while($row=$result->fetch_array()){ $items.=<<<EOD { "r": "{$row['first_name']}", }, EOD; $counter++; } header('Content-type: application/json'); ?> { "response": [ <?php echo $item;?> ] } <?php } else { echo json_encode("0 results"); } $conn->close(); } ?> 500 error
no you can't do that... before you have to insert the ELSE Code and in the response you write only { "response": [ <?php echo $item;?> ] } .... Now try this code without exceptions..
|

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.