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 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    23
    Thanks
    11
    Thanked 1 Time in 1 Post

    passing database records to javascript as arrays

    Hi,
    I am working on a javascript application that requires use of a mysql database i have made.
    so what I want to be able to do is to use the values in my database in my javascript. is the best way to do this to put the data into arrays to pass to javascript? if not what is the better way?

    I was thinking of making each row of the table an array with the first field as the array name. How would I go about doing that? can I do that with MYSQL_FETCH_ROW()?
    Thanks a lot,
    Dan

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    80
    Thanks
    0
    Thanked 13 Times in 12 Posts
    I would loop through the data storing it as a multidimensional array and then package this up with json_encode()

    json is pretty easy to parse with jquery and is pretty portable.

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    23
    Thanks
    11
    Thanked 1 Time in 1 Post
    Quote Originally Posted by MarkR View Post
    I would loop through the data storing it as a multidimensional array and then package this up with json_encode()

    json is pretty easy to parse with jquery and is pretty portable.
    So how would I get the arrays back in javascript?
    So far I have this which gives me the json_encode strings.
    <code>
    $result = mysql_query("SELECT * FROM courses");

    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    echo $row[0] . " " . $row[1] . " " . $row[2]. " " . $row[3]. " " . $row[4]. " " . $row[5]. " " . $row[6]. " " . $row[7]. " " . $row[8];

    echo json_encode($row);
    echo "<br />";
    }
    </code>

    in order to be able to use the arrays in javascript, how do i decode the json strings into javascript arrays?


  •  

    Posting Permissions

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