0

Previously I've successfully accessed a php array in a call back jquery ajax function by doing the following:

$.post('ajax.php', {name: name}, function(data) {

                var obj = jQuery.parseJSON(data);

                var item = obj[6]

With the php being:

$sql = "SELECT * FROM sales WHERE email = 'test' AND item = '$name' ";
$result = mysqli_query($conn, $sql);

    if ($result){
                    $row = mysqli_fetch_array($result);
                    echo json_encode($row);
                    }

I have now the slightly different situation which i wouldn't have thought a problem but is, very much:

PHP

$sql = "SELECT * FROM sales WHERE ".$searchwithin." BETWEEN '2017-02-01' AND     '2017-03-01'";

$result = mysqli_query($conn, $sql);
    if ($result){   
                    while ($row = mysqli_fetch_assoc($result)){

                    echo json_encode($row);
                    }

When I do the following Jquery:

 var obj = data.length;

 alert(obj);

I get a ridiculously high number which tells me that it's counted the string length and not the array length; so I guess that's the first problem, despite seemingly copying a successful past ajax, i am now getting a string returned and not an array.

Finally, and I guess this relates to the first problem, whenever I use

var obj = jQuery.parseJSON(data);

it doesn't like it at all; i either get nothing, not even an alert, or i do get an alert with "undefined" returned.

The array should contain eight or so mysqli table rows with fifteen columns; which i believe i then access with something like e.g. obj[5][7]. But I can't try that yet because it's returning a string, it seems, not an array. I'm new to coding by the way and thanks in advance.

Oh and this is what i get if i just do alert(data):

{"id":"8", etc }

4
  • json_encode must be used once Commented Mar 6, 2017 at 21:04
  • oh i didn't know that lol. so i create a variable right, to store the array during the loop and then do json_encode on the variable at the end? Commented Mar 6, 2017 at 21:05
  • Yes, it probably works in your fist code because maybe only 1 row is returned. Build an array in the loop and encode/echo after. Commented Mar 6, 2017 at 21:21
  • You might also consider no loop: echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC)); Commented Mar 6, 2017 at 21:23

1 Answer 1

2

when you decode ajax in loop you have many objects instead of one. Your ajax get response something like {some rom}{some row1}{some row2}

you need to create array and push data in loop into it, and decode it's array

$myArray = array();
if ($result = $mysqli->query("SELECT * FROM phase1")) {

    while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $myArray[] = $row;
    }
    echo json_encode($myArray);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for this; am i right to think this array will be a two dimensional array; if so, how to i get say, the nth row for the nth column? i've tried - in my jquery - var obj = jQuery.parseJSON(data); alert(obj[5]); - but this just gives me [object Object]? Nor do two indexes help...
@user1849962 dump data - console.log(data) to see array structure, in your case in obj[5] return object witch contain your db row, so obj[5]['columname'] get your specific column row value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.