-2

I thought this would be much simpler but I'm having some difficulty finding a simple answer online.

I simply want to get data from an MySQL table with ajax and transform it into a js array.

My table is ultra simple.. it's just:

Table 1
 id     value
  1     1 
  2     2 
  3     3

What is the best way to do this?

So far I have the php file:

while($row = mysql_fetch_array($query)) {$array[] = $row;}

which emits something like:

Array ( [0] => Array ( [0] => 7 [idGlobal] => 7 [1] => 4.9 [tc] => 4.9 ) ) Array ( [0]     => Array ( [0] => 7 [idGlobal] => 7 [1] => 4.9 [tc] => 4.9 ) [1] => Array ( [0] => 3     [idGlobal] => 3 [1] => 2.2 [tc] => 2.2 ) ) Array ( [0] => Array ( [0] => 7 [idGlobal] => 7 [1] => 4.9 [tc] => 4.9 ) [1] => Array ( [0] => 3 [idGlobal] => 3 [1] => 2.2 [tc] => 2.2 ) [2] => Array ( [0] => 5 [idGlobal] => 5 [1] => 1.9 [tc] => 1.9 ) ) Array ( [0] => Array ( [0] => 7 [idGlobal] => 7 [1] => 4.9 [tc] => 4.9 ) [1] => Array ( [0] => 3 [idGlobal] => 3 [1] => 2.2 [tc] => 2.2 ) [2] => Array ( [0] => 5 [idGlobal] => 5 [1] => 1.9 [tc] => 1.9 ) [3] => Array ( [0] => 6 [idGlobal] => 6 [1] => 1.9 [tc] => 1.9 ) ) Array ( [0] => Array ( [0] => 7 [idGlobal] => 7 [1] => 4.9 [tc] => 4.9 ) [1] => Array ( [0] => 3 [idGlobal] => 3 [1] => 2.2 [tc] => 2.2 ) [2] => Array ( [0] => 5 [idGlobal] => 5 [1] => 1.9 [tc] => 1.9 ) [3] => Array ( [0] => 6 [idGlobal] => 6 [1] => 1.9 [tc] => 1.9 ) [4] => Array ( [0] => 4 [idGlobal] => 4 [1] => 1.6 [tc] => 1.6 ) )

but I still don't know the best way to get it with ajax to a JS array

coiso
  • 7,151
  • 12
  • 44
  • 63
  • 3
    Have you tried something already? Could you show us your code? – Alfabravo Oct 18 '12 at 20:38
  • If you got a problem like this, break it up. There's a couple of layers between MySQL and Javascript. Just think 30 second about what they are and you should be able to find... Ray's answer. Then it should be easy to find the partial solutions to get the whole. – GolezTrol Oct 18 '12 at 20:42

3 Answers3

3
$query = "SELECT id, value FROM table";
$data = array();

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}

header('Content-Type: application/json');
echo json_encode($data);
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
2
  1. Query from mysql
  2. Fetch Result oas associative array
  3. json encode the array
  4. return json string to the javascript code.
Ray
  • 40,256
  • 21
  • 101
  • 138
1

Use php function json_encode to create string containing json. Later in javascript when string retrieved, easiest way to turn that into a Array something like this.

var myArray = eval('(' + jsonStringRetrieved + ')');

And check out these too how to use json_encode Safely turning a JSON string into an object

Community
  • 1
  • 1
Janar Jürisson
  • 510
  • 5
  • 16
  • If you Ajax using JQuery (which does make life easier), it will already process the response to a Javascript object. A little safer than using eval. – GolezTrol Oct 18 '12 at 20:45
  • which jquery ajax function is best for this situation? – coiso Oct 18 '12 at 20:52