On Wed, 5 Nov 2003 14:53:15 +0800, you wrote:

>I am trying to get the results of a function, which queries MySQL, back into
>an array for me to order. I only want to print out certain fields (I call
>the same query, but use different fields in different places).

>Can someone perhaps show me how to do it (or point me in the right
>direction), since I need to do this:

It depends on exactly how your data is formatted, which you don't tell us.
I'm going to assume your query returns two values, id and name, and you want
them returned as a dictionary.

Probably the simplest way is to build an array as you iterate over the
query, then return it. Something like:

function ReturnSQLResults() {

....................

        $result = array ();

        $rs = $mysql_query($sql);

        if ($rs == FALSE)
        {
                return (NULL);
        }

        while (($row = mysql_fetch_array ($rs)) != FALSE)
        {
                $result[$row['id']] = $row['name'];
        }

        if (sizeof ($result) == 0)
        {
                return (FALSE);
        }

        return ($result);
}

You now have a function that returns NULL on error, FALSE if no records were
found, else an array of (id => name)

If you're returning more than two values, you'll probably want your array
structure to be:

(id => array(firstname, lastname))

in which case try

$result [$row['id']] = array ($row['firstname', $row['lastname']);

if you just want a straight array-of-arrays, something like

((id, firstname, lastname), (id, firstname, lastname))

then

$result[] = array($row['id', $row['firstname', $row['lastname')

will do it.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to