Hello. Tell me please! I collect data from the database and form an associative array.

<?php mysql_connect("localhost", "root", ""); mysql_select_db("test_basa"); $o = mysql_query("SELECT * FROM test_table"); while($result = mysql_fetch_assoc($o)) { $arr[] = array($result['name'], $result['date'], $result['que']); } //var_dump($arr); echo json_encode($arr); ?> 

Next, on the client side I make an ajax request:

 $.ajax({ async: true, type: "POST", url: "/ajax_que.php", dataType: "json", success: function(data_db) { var answer = JSON.parse(data_db); console.log(data_db); } }); 

Error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I understand that this is because we get an object.

I also need to get an associative array from the above php-file so that later it can be used in javascript in the same form. That is, it is formed, for example, in php: [['one', 'two'], ['ten', 'eleven']], and in the same form I need this array in javascript :) How to do this?

  • You already get a json jar. so use right away without JSON.parse just data_db - Naumov
  • @Naumov That is, the answer already contains an associative array, or does it need to be converted? - Natali

0