I send a post-request to the server, and php responds with an array, but I cannot extract the values ​​from the array

I am transmitting 2 values ​​from a php server to js like this (PHP):

$idp = mysqli_fetch_array(mysqli_query($db, "SELECT idp FROM videos WHERE idv = '$idv'"))['idp']; $p = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM accounts WHERE idp = '$idp'"))['login']; $data = [ 0 => $idp, 1 => $p ]; $data = json_encode($data); echo($data); 

And I extract it like this:

 function(arr){ var idp = arr[0]; var p = arr[1]; 

But I get the wrong values, what I convey, what is wrong and how to fix it?

  • and the fact that you returned to the console you tried to withdraw? mb mb? - Rostyslav Kuzmovych

1 answer 1

The resulting arr needs to be driven through json.parse (arr). And when the idv and idp fields in the database are not unique, it is better to add LIMIT 1 to the queries. Well, through the f12 key in chrome, for example, see what comes up.

  • Error: json is not defined code: arr = json.parse(arr); - Ivan Repin
  • In internet explorer? - Denis Nesteruk
  • I apologize, JSON instead of json - Denis Nesteryuk
  • Okay, now for the place of values ​​there [object Object] , the values ​​can not be extracted, displays NaN - Ivan Repin