0

I am trying to fetch data from MySQL table that have 2 columns, Temperature and Value. I want to store these values to JSON and then to pass to the client side script. My PHP code is: database2json.php:

<?php
    $con = mysql_connect("localhost", "root", "123456");
    if (!$con) {
        die('Could not connect:' . mysql_error());
    }
    mysql_select_db("klima", $con);
    $result = mysql_query("select Dan, Temperatura from TEMPERATURA");
        $niz = array();
    while ($row = mysql_fetch_array($result)) {
        $niz[$row['Dan']] = $row['Temperatura'];
    }
        mysql_close($con);
        $obj = json_encode($niz);
        echo $obj;
?>

When I run this file on server I get this:

{"1":"-1","2":"0","3":"0","4":"0","5":"4","6":"5","7":"3","8":"2","9":"2","10":"1","11":"-2","12":"-2","13":"0","14":"1","15":"-2","16":"-1","17":"-1","18":"-2","19":"-1","20":"3","21":"-1","22":"0","23":"1","24":"3","25":"1","26":"1","27":"-1","28":"-1","29":"4","30":"5","31":"5"}

That is what is expected.

Html is nothing special.

index.html:

<html>
    <head>
        <title>jQuery</title>
        <script src="jquery.js" type="text/javascript"></script>
        <script src="custom.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="id1"></div>
    </body>
</html>

Now I call php from jQuery and to show these values.

custom.js:

$(document).ready(function(){
    $.post('database2json.php', function(data){
        $('#id1').html(data);
    },
    "json");
});

This also gives same output like php:

{"1":"-1","2":"0","3":"0","4":"0","5":"4","6":"5","7":"3","8":"2","9":"2","10":"1","11":"-2","12":"-2","13":"0","14":"1","15":"-2","16":"-1","17":"-1","18":"-2","19":"-1","20":"3","21":"-1","22":"0","23":"1","24":"3","25":"1","26":"1","27":"-1","28":"-1","29":"4","30":"5","31":"5"}

Now I dont know how to convert this into array of [Dan, Temperatura]. I need this array to forward to chart and plot data (I am not asking about plotting, just to get array).

How to achieve this?

3 Answers 3

5

Your output

{"1":"-1","2":"0","3":"0",...,"31":"5"}

Is a JavaScript object in its current form. You can simply access it as:

alert(data["1"]);
// -1

alert(data["31"]);
// 5

Note that the common syntax for object literals is dot notation: object.propertyname, but that will not work for numeric property names like your 1-31 indexes. So instead you use the bracketed property name as in data["1"].

If you really need it as an indexed array, you can convert it as:

var array = [];
for (key in data) {
  array[key] = data[key];
}
// Now array is an Array with similar structure to the object data

Update

There is another possibility to get this data as a proper array directly from PHP. You can wrap the output in an additional array like this:

// Wrap the array in another array indexed as niz
$obj = json_encode(array("niz" => $niz));
echo $obj;
Sign up to request clarification or add additional context in comments.

1 Comment

While it is an object, it is actually sent to JavaScript as a string, jQuery then turns it into an object, and passes it on as an object into the first argument of the complete function (in this case data).
1

I have something like your code in my project. It is like this:

$final = array('msg' => $msg, 'result' => $result);
echo json_encode($final);

As you see, I made an array with 2 key and value. This code works fine for me. try to obey the method above to create your array and test it again. I hope you can solve your problem.

Comments

0

If I understand correctly, you want to convert PHP's JSON output to an actual array, right?

You can simply use eval() for that.

myArray = eval(data);

WARNING: Eval is considered unsafe and you should only use it for trusted sources. Make sure that there is absolutely no way for anything else than your PHP script to be evaluated.

5 Comments

He wants to convert it to a javascript-array at client site, not PHP.
I want to transfer JSON between PHP and jQuery and on the jQuery side make array from that JSON.
So you already have an array? In that case: what is your question?
I didn't know that this is array, I thought that this is some kind of JSON array.
Fun fact: JSON means Javascript object notation. If you ask Javascript to write an object to the screen, it will always use JSON - that's what it was built for, serializing javascript objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.