Creating a javascript array from database data

From: Date: Wed, 15 Aug 2001 16:16:13 +0000
Subject: Creating a javascript array from database data
Groups: php.general 
Request: Send a blank email to php-general+get-62863@lists.php.net to get a copy of this message
Hi there, Well after a few hours roaming around various websites I am at a loss. Here is what I am trying to do: 1) Access a MySQL database which contains 1 table 2) Read the records from this table 3) Store the values returned from this table into javascript array elements, ie, if I get the values "dog", "cat" and "cow" back I want these stored in an array as such: myArray[0] = valueReturned1 myArray[1] = valueReturned2 myArray[2] = valueReturned3 You get the idea. Problem being that I cannot work out how to implement the javascript section of this. At the moment my php script writes the values returned from the database to screen but I require these to be stored in a javascript array. Please can someone help me before I go mad :) Here is my current .php script: ---------------------------------------------- <html> <head> <title>Menus test</title> </head> <body bgcolor="white"> <?php $dbhost = 'localhost'; $dbuser = 'guest'; $dbpass = 'guest'; $dbname = 'IFE'; $dbtable = 'menus'; //------ DATABASE CONNECTION --------// mysql_connect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to database"); mysql_select_db($dbname) or die ("Unable to select database"); $sql = "SELECT * FROM $dbtable"; $result = mysql_query($sql); $number = mysql_numrows($result); $i = 0; if ($number == 0) print "Error - No records found"; elseif ($number > 0) { while ($i < $number) { $text = mysql_result($result, $i, "name"); print "$text"; $i++; } } mysql_free_result($result); mysql_close(); ?> </body> </html> ---------------------------------------------- Thanks, Neil -------------------------------- Email: neil.freeman@asa.co.uk neil_freeman@yahoo.com --------------------------------

« previous php.general (#62863) next »