Re: SQL query to array?

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

 



On Wed, April 26, 2006 1:39 am, William Stokes wrote:
> Can someone please help me to put the results of a query to an array.

There are only a few thousand tutorials on this on the 'net...

> 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) {

If you do not start indenting your code correctly now, you will most
likely never enjoy much success in your programming efforts.

> $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));

While there is nothing technically "wrong" with this, it's going to
make life harder for you to deal with the "Team$cur" index.

Much easier to do:
$arr[] = $row;

to append to the array.
>
> $cur++;
> }

Or did I miss the part where you described what you saw and what you
expected to see?

Because what you posted is "fine" as far as it goes, as far as I can
tell.

-- 
Like Music?
http://l-i-e.com/artists.htm

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