0

Very simple: I am using a prepared statement to select data and return it in a json_encode form.

The problem: Instead of receiving a JSON string full of the returned data, I am getting something like this:

[true, true, true, true]

My guess is that it is checking whether every value is set and then it is just returning whether the value is set or not, in a boolean form.

$stmt = $connection->prepare("SELECT * FROM details WHERE age = ?");
$stmt->bind_param('i', $age);
$stmt->execute();
$json = array();
while($row = $stmt->fetch()){
$json[] = $row;
}

echo json_encode($json);

This is the AJAX that I am using.

$(document).ready(
    function () {
        $('#call_back_btn').click(function() {
            $.post("process.php", {
                name:   $('#name').val(),
                age:    $('#age').val(),
                value:  $('#value').val(),
                task:   "submit_prepared"
            }, 
            function(data) {
                alert(data);
            })
        })
    });

What should I do so that my data is returned into a JSON string? Thank you.

3
  • Try to use data=JSON.parse(data); before alerting it. Commented Oct 13, 2013 at 17:48
  • @VedantTerkar why does it suck? How can I rewrite, lets say, the name: part of the AJAX and use JSON.parse(data) instead? Commented Oct 13, 2013 at 17:50
  • @VedantTerkar it is still returning a boolean json string. I inserted the 'data=JSON.parse(data);' right before the alert. Commented Oct 13, 2013 at 17:56

2 Answers 2

3

Please have a look at the documentation for $stmt->fetch(). It is completely correct that you receive booleans. Modified the code to use bind_result.

$stmt = $connection->prepare("SELECT name, age FROM details");
$stmt->execute();
$stmt->bind_result($name, $age);
$json = array();
while($stmt->fetch()){
  $json[] = array("name" => $name, "age" => $age);
}

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

7 Comments

Now with this I am getting a [object Object], which is... (My guess) Good right? Now... How can I make it give me the information? Is there anything else I should do?
@TiffanyLowe I think you will get a result-set by calling get_result. You will need to use $resultset->fetch_array() to iterate through the rows in this result-set.
@Markus I don't quite get that. If it is not too much to ask, can you re-write the piece of code that you pointed out with the one that you suggested?
I added the code to my answer. Unfortunately this is untested, since I don't have any environment to test it currently.
@Markus yes. I tried it. It does not return anything now. This is frustrating. I will try to see if I wrote anything wrong.
|
1

Try to print the $json from the php just to make sure that the array is correct. Then print the json_encode($json) variable in php again, without ajax. I generally hard code the query and run it from the browser directly. This way you will make sure that the correct data is being generated by php.

Also try using the $.ajax function. This way you will have more control.

$.ajax({
    url: 'process.php',
    type: 'POST',
    dataType: 'JSON',
    data: {YOUR DATA GOES HERE},
    success: function(data){
            alert(data);
     }
});

1 Comment

You can set the dataType with $.post.

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.