...

View Full Version : passing database records to javascript as arrays



dan9933
04-24-2012, 02:20 AM
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

MarkR
04-24-2012, 11:41 AM
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.

dan9933
04-24-2012, 06:57 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum