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 }
json_encodemust be used onceecho json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));