0

Need help forming javascript objects from MySQL rows of data. I'm using IE9 and Chrome on Windows-7.

I've managed to get what I believe to be an array (of objects) in Javascript from mySQL data. I can use alerts to see the whole array, as well as one individual object, as in my code.

What I cannot do yet is navigate a particular object's properties (the column values of a particular row in the database).

What I need to do is iterate through myObjects, and use the property values in each to create some graphics. I also need to be able to retrieve each object's properties at any time going forward as well.

UPDATE: including my php located in head html object:

    <?php
    //------------------- constants --------------------
    $objects = array();
    $jsonData = "";
    //------------------- database connection ----------
    $data_source = 'mysql:host=localhost;dbname=myDB';
    $db_user = 'root';
    $db_password = 'password';
    $conn = new PDO($data_source, $db_user, $db_password,
              array(PDO::ATTR_EMULATE_PREPARES => false,
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_PERSISTENT));
    //prepare query
    $stmt = $conn->prepare("SELECT * FROM tblbranchstatus");
    $stmt->execute();
    //fetch each row of results
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $rows[] = json_encode($row);
    }
?>

var ART = {};
//capture data from database as json string data
ART.strJSON = <? php echo json_encode($rows); ?> ;
//capture json string data as array of javascript objects
//using 'eval' cause I know this data's source and I couldn't get JSON.parse to work
ART.myObjects = eval(ART.strJSON);
ART.branch = ART.myObjects[6];
alert(ART.branch); // this gives me the expected object {"a":"aa", "b":"bb"...}
alert(ART.branch.a); // can't retrieve the property - gives me 'undefined'
5
  • Could you post an example output for json_encode($rows);? Commented May 6, 2013 at 21:44
  • You shouldn't need JSON.parse or eval. Valid JS should be emitted. Commented May 6, 2013 at 21:45
  • 1
    alert( ART.strJSON[6].a ) might surprise you... Commented May 6, 2013 at 21:56
  • @bfavaretto the output for json_encode($rows); is ["{\"osj\":\"40D\",\"region\":\"FID\",...},{...}, "] So, it looks like it might actually be one long string, as hinted by others. I'm kind of lost as to how to do this. Commented May 8, 2013 at 1:40
  • @dandavis alert(ART.strJSON[6].a) gives me undefined, I guess due to this unexpected string wrap around my JSON. Commented May 8, 2013 at 1:47

2 Answers 2

2

This doesn't look like the right thing to do. Here's what you should be doing:

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $rows[] = $row;
}

Don't do json_encode() on each row.

ART.myObjects = <?php echo json_encode($rows); ?>;

You can immediately use the output of json_encode($rows) in your script.

Update

As rightfully mentioned by bfavaretto, you can make this even shorter by encoding all rows in one go:

ART.myObjects = <?php echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); ?>;
4
  • Makes sense, but I just get [object Object],... from alert(ART.myObjects); Commented May 8, 2013 at 2:16
  • @csupak Don't use alert() to debug! Use console.log() instead. Commented May 8, 2013 at 2:17
  • 1
    @csupak You could also use json_encode($stmt->fetchAll((PDO::FETCH_ASSOC)), so the while loop is not even needed. The problem is clear now, you were double encoding your array. Commented May 8, 2013 at 2:22
  • Yes, this works! Thanks so much. Note the comment version here has an extra paren, but Jack's update above is correct. Thanks again to everyone! Commented May 8, 2013 at 2:44
0

I would check if ART.branch is actually a string with the JSON notation rather than the actual object.

1
  • Yes, this might be the issue. When I echo from php, I'm getting the whole shebang wrapped with ["..."]. I've updated my post with my php code as well. Can you help me get my head around this? Commented May 8, 2013 at 1:53

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.