Re: SQL query to array?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxx> 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
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux