Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-21-2003, 09:16 PM   PM User | #1
mmikkelson
Registered User

 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mmikkelson is an unknown quantity at this point
sql query results to js array?

Is it possible to dynamically drop the results of a SQL query into a javascript array?
mmikkelson is offline   Reply With Quote
Old 10-21-2003, 10:50 PM   PM User | #2
speedracer
Regular Coder

 
Join Date: May 2002
Location: Seattle, WA
Posts: 665
Thanks: 0
Thanked 0 Times in 0 Posts
speedracer is an unknown quantity at this point
I'll move this question to javascript forum.
speedracer is offline   Reply With Quote
Old 10-22-2003, 12:06 AM   PM User | #3
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
You could do a loop through all of the results and populate the JS array like that.

Hope that helps!

Happy coding!
nolachrymose is offline   Reply With Quote
Old 10-22-2003, 12:54 AM   PM User | #4
mr_ego
Regular Coder

 
Join Date: Jun 2002
Location: Brisbane, Australia
Posts: 181
Thanks: 1
Thanked 0 Times in 0 Posts
mr_ego is an unknown quantity at this point
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.
mr_ego is offline   Reply With Quote
Old 10-23-2003, 10:11 PM   PM User | #5
mmikkelson
Registered User

 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mmikkelson is an unknown quantity at this point
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
mmikkelson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:04 AM.


Advertisement
Log in to turn off these ads.