Re: [PHP] Return mysql_fetch_array($result) from a function

2003-11-05 Thread David Otton
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



Re: [PHP] Return mysql_fetch_array($result) from a function

2003-11-05 Thread Chris Shiflett
--- Terence [EMAIL PROTECTED] 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).
 
 This works is I print out the fields in the function itself:
 
 while($row = mysql_fetch_array($result)) {
 echo $row[id] . br;
 }

So why do you not want to use this method? Does it not do what you need?


 This however prints out an unending list of values:
[snip]
 $mysql_query = mysql_fetch_array($result);
 
 while($row = $mysql_query) {
 echo $row[id] . br;
 }

$row here is being set to a result set. You are querying MySQL (with an
empty query, something I've never tried), and until MySQL fails, this will
run forever.

You probably want to run your query once and then loop through the result
set. Mind your variable names as well, because your use of $row above adds
to the confusion.

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

Use things like that. You can also make sure $result is not false prior to
using it, because that is indicative of a failed query.

Hope that helps.

Chris

=
My Blog
 http://shiflett.org/
HTTP Developer's Handbook
 http://httphandbook.org/
RAMP Training Courses
 http://www.nyphp.org/ramp

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



[PHP] Return mysql_fetch_array($result) from a function

2003-11-04 Thread Terence
Hi All,

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

This works is I print out the fields in the function itself:

while($row = mysql_fetch_array($result)) {
echo $row[id] . br;
}

This however prints out an unending list of values:

/* This is where I think it's getting messed up, since it doesnt seem to be
able to store the query in a variable. I suspect it's something to do
recursive and multi-dimension arrays
http://www.phpbuilder.com/manual/language.types.array.php*/

$mysql_query = mysql_fetch_array($result);

while($row = $mysql_query) {
echo $row[id] . br;
}


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

function ReturnSQLResults() {


$mysql_query = mysql_fetch_array($result);
return $mysql_query;
}

And then be able to just print the fields I require.

Thanks in advance!!!

Terence

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