PDA

View Full Version : sql query results to js array?



mmikkelson
10-21-2003, 09:16 PM
Is it possible to dynamically drop the results of a SQL query into a javascript array?

speedracer
10-21-2003, 10:50 PM
I'll move this question to javascript forum.

nolachrymose
10-22-2003, 12:06 AM
You could do a loop through all of the results and populate the JS array like that.

Hope that helps!

Happy coding! :)

mr_ego
10-22-2003, 12:54 AM
This is theroretical, not guarenteed.




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?>";
<?
}
}

?>

mmikkelson
10-23-2003, 10:11 PM
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