2013-05-06 21:42
浏览 46


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:

    //------------------- 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,
    //prepare query
    $stmt = $conn->prepare("SELECT * FROM tblbranchstatus");
    //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'

图片转代码服务由CSDN问答提供 功能建议

需要帮助从 MySQL 数据行中形成 javascript 对象 。 我在 Windows-7 上使用 IE9 Chrome

我已经设法从mySQL数据中获取我认为是Javascript中的数组(对象)。 我可以使用警报来查看整个数组,以及一个单独的对象,就像在我的代码中一样。


我需要做的是遍历 myObjects ,并使用每个属性值来创建一些图形。 我还需要能够随时检索每个对象的属性。

更新:包括位于head html对象中的php: < pre> &lt;?php // -------------------常量----------------- --- $ objects = array(); $ jsonData =“”; // -------------------数据库连接----- ----- $ 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 =&gt; false, PDO :: ATTR_ERRMODE =&gt; PDO :: ERRMODE_EXCEPTION, PDO :: ATTR_PERSISTENT)); //准备查询\ n $ stmt = $ conn-&gt; prepare(“SELECT * FROM tblbranchstatus”); $ stmt-&gt; execute(); //获取每行结果 而($ row = $ stmt-&gt ; fetch(PDO :: FETCH_ASSOC)){ $ rows [] = json_encode($ row); } ?&gt; var ART = {}; //从数据库中捕获数据为js 在字符串数据 ART.strJSON =&lt;? php echo json_encode($ rows); ?&GT; ; //将json字符串数据捕获为javascript对象数组 //使用'eval'因为我知道这些数据的来源而我无法使JSON.parse工作 ART.myObjects = eval(ART.strJSON) ; ART.branch = ART.myObjects [6]; alert(ART.branch); //这给了我预期的对象{“a”:“aa”,“b”:“bb”...} alert(ART.branch.a); //无法检索属性 - 给我'未定义'

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtao4319 2013-05-08 01:55

    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.


    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)); ?>;
    打赏 评论
  • doufeng5059 2013-05-06 21:49

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

    打赏 评论

相关推荐 更多相似问题