Re: SQL query to array?

From: Date: Wed, 26 Apr 2006 07:43:36 +0000
Subject: Re: SQL query to array?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-234787@lists.php.net to get a copy of this message
Ooopps. I just did have array too many. This did the append corretly: $arr["Team$cur"] = array(id => $id, sort => $srt, jnimi => $nimi); -Will ""Joe Wollard"" <joe.wollard@gmail.com> kirjoitti viestissä:4b95b27e0604252357y224eb6e4xf351bbdb4593bdb3@mail.gmail.com... Will, Seems to me like you've just done it! Here's another way of doing it that will utilize mysql_fetch_assoc() to allow your query to dictate the elements of the array. Keep in mind, I haven't tested this, but since I'm not entirely sure what you are asking the list, I'll offer it anyway ;-) <?php /*...do connection stuff here...*/ $sql = "SELECT sortteri,jouk_id,jouk_nimi FROM x_table ORDER BY sortteri ASC"; $result = mysql_query($sql); $num = mysql_num_rows($result); $Team = null; //We'll fill this up later - gotta love PHP ;-) // loop over each row in the result set for($i=0; $i<$num; $i++){ $tmp = mysql_fetch_assoc($result); // loop over each column in the current row while(list($key, $val) = each($tmp)) $Team[$key][$i] = $val; } // display the contents of the $Team array print_r($Team); ?> On 4/26/06, William Stokes <kalles@operamail.com> wrote: > > Hello, > > Can someone please help me to put the results of a query to an array. > Never > done this before: > > $sql = "SELECT sortteri,jouk_id,jouk_nimi FROM x_table ORDER BY sortteri > ASC"; > $result = mysql_query($sql); > $num = mysql_num_rows($result); > $cur = 1; > > //create empty array that contains an array before loop > $arr = array("Team" => array()); > > while ($num >= $cur) { > $row = mysql_fetch_array($result); > $id = $row["jouk_id"]; > $srt = $row["sortteri"]; > $nimi = $row["jouk_nimi"]; > > //append values to the array > $arr = array("Team$cur" => array(0 => $id, 1 => $srt, 2 => $nimi)); > > $cur++; > } > > Thanks > -Will > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.general (#234787) next »