Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sql query results to js array?

    Is it possible to dynamically drop the results of a SQL query into a javascript array?

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Seattle, WA
    Posts
    665
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'll move this question to javascript forum.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could do a loop through all of the results and populate the JS array like that.

    Hope that helps!

    Happy coding!

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Brisbane, Australia
    Posts
    181
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is theroretical, not guarenteed.

    Code:
    var jsarray = new Array();
    
    <?
    
    $sql = "DESCRIBE table";
    $query = mysql_query($sql);
    
    while ($_SQL = mysql_fetch_array($query)) {
       $_ITEM[$_SQL['field']] = $_SQL;
    }
    
    $sql = "SELECT * FROM table";
    $query = mysql_query($sql);
    
    $i = -1;
    
    while ($_SQL = mysql_fetch_array($query)) {
       ++$i;
       echo "jsarray[$i] = new Array();";
       foreach ($_ITEM AS $fieldname => $_FIELD) {
          echo "jsarray[$i][$fieldname] = "{$_SQL[$fieldname]}";
       }
    }
    
    ?>

    Like i said. 100% theroretical.

    You could try this too...

    <?

    $sql = "SELECT * FROM table";
    $query = mysql_query($sql);

    $i = -1;

    while ($_SQL = mysql_fetch_array($query)) {
    ++$i;
    ?>
    jsarray[<?php echo $i?>] = new Array();
    <?
    foreach ($_SQL AS $fieldname => $fieldvalue) {
    ?>
    jsarray[<?php echo $i?>][<?php echo $fieldname?>] = "<?php echo $fieldvalue?>";
    <?
    }
    }

    ?>
    -mR_eGo
    _______________________
    Programming since
    3 years old.

  • #5
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks to both of you
    i'm very new to javascript and i figured i could drop the results of a SQL query into an array, but wasn't sure about how to go about it.

    thanks for the suggestions and example


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •