[PHP-DB] Merge result set of query?

2005-03-09 Thread ioannes
My first attempt was to use $result=array_merge($result1,$result2) - doesn't 
work.  'not a valid resource'

I have two databases with different connections, so MySQL UNION query would 
not work.

How do I merge the result set of the queries in that case?
John 

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


Re: [PHP-DB] Merge result set of query?

2005-03-09 Thread Martin Norland
ioannes wrote:
My first attempt was to use $result=array_merge($result1,$result2) - 
doesn't work.  'not a valid resource'

I have two databases with different connections, so MySQL UNION query 
would not work.

How do I merge the result set of the queries in that case?
You're going to have to merge the results within PHP.
not a valid resource is an error you get when you try to read from a 
result set and what you pass it isn't a resource for a result set.

I think you're doing something along the lines of:
$result1 = query_database_one($somequery);
$result2 = query_database_two($someotherquery);
$result = array_merge($result1, $result2);
while (mysql_fetch_row($result)) {
// foo
}
...
Which won't work on any level.  mysql_fetch_row takes a resource, it's a 
pointer to give the database to retrieve the row.  When you do your 
query to the database, the database does the query but doesn't return 
the full result - instead it just returns a resource to the result, so 
you can get it as you need it.

I suggest you start from that information and work forward.
cheers,
--
- Martin Norland, Sys Admin / Database / Web Developer, International 
Outreach x3257
The opinion(s) contained within this email do not necessarily represent 
those of St. Jude Children's Research Hospital.

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


Re: [PHP-DB] Merge result set of query?

2005-03-09 Thread Jochem Maas
Martin Norland wrote:
ioannes wrote:
My first attempt was to use $result=array_merge($result1,$result2) - 
doesn't work.  'not a valid resource'

I have two databases with different connections, so MySQL UNION query 
would not work.

How do I merge the result set of the queries in that case?

You're going to have to merge the results within PHP.
not a valid resource is an error you get when you try to read from a 
result set and what you pass it isn't a resource for a result set.

I think you're doing something along the lines of:
$result1 = query_database_one($somequery);
$result2 = query_database_two($someotherquery);
$result = array_merge($result1, $result2);
while (mysql_fetch_row($result)) {
while reading I thought, would this:
while (($row = mysql_fetch_row($result)) || ($row = mysql_fetch_row($result2)))
{
// just do it. 
}
.. work (due to shortcircuiting)? and how ugly is it?
// foo
}
...
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP-DB] Merge result set of query?

2005-03-09 Thread Martin Norland
Jochem Maas wrote:
Martin Norland wrote:
ioannes wrote:
My first attempt was to use $result=array_merge($result1,$result2) - 
doesn't work.  'not a valid resource'

I have two databases with different connections, so MySQL UNION query 
would not work.

How do I merge the result set of the queries in that case?

You're going to have to merge the results within PHP.
not a valid resource is an error you get when you try to read from a 
result set and what you pass it isn't a resource for a result set.

I think you're doing something along the lines of:
$result1 = query_database_one($somequery);
$result2 = query_database_two($someotherquery);
$result = array_merge($result1, $result2);
while (mysql_fetch_row($result)) {

while reading I thought, would this:
while (($row = mysql_fetch_row($result)) || ($row = 
mysql_fetch_row($result2)))
{
// just do it. 
}

.. work (due to shortcircuiting)? and how ugly is it?
// foo
}
That would work if you just wanted to iterate over both resultsets and 
do the same thing, yes. Say, if you had lists of users who signed up for 
something in two separate databases, you could print them all out with 
that.  Since he said union and not join - that is probably his intention.

I can't decide if that code is beautiful or horribly ugly though...  I 
would have to say it's beautiful so long as the 'just do it' is very 
short - if the 'just do it' should be a function - then you should just 
suck it up and do two while's.  Also, for every result in $result2, you 
have to poke at the first database and say any more yet?, which is 
hackish.

slick 'obfuscated perl' entry type fix though :) [okay, it's not 
obfuscated in the traditional sense]

cheers,
--
- Martin Norland, Sys Admin / Database / Web Developer, International 
Outreach x3257
The opinion(s) contained within this email do not necessarily represent 
those of St. Jude Children's Research Hospital.

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


Re: [PHP-DB] Merge result set of query?

2005-03-09 Thread Jochem Maas
Martin Norland wrote:
Jochem Maas wrote:
Martin Norland wrote:

while (($row = mysql_fetch_row($result)) || ($row = 
mysql_fetch_row($result2)))
{
// just do it. 
}

.. work (due to shortcircuiting)? and how ugly is it?
// foo
}
That would work if you just wanted to iterate over both resultsets and 
do the same thing, yes. Say, if you had lists of users who signed up for 
something in two separate databases, you could print them all out with 
that.  Since he said union and not join - that is probably his intention.
that was what I had in mind when it popped into my head :-)
I can't decide if that code is beautiful or horribly ugly though...  I 
would have to say it's beautiful so long as the 'just do it' is very 
short - if the 'just do it' should be a function - then you should just 
suck it up and do two while's.  Also, for every result in $result2, you 
have to poke at the first database and say any more yet?, which is 
hackish.

slick 'obfuscated perl' entry type fix though :) [okay, it's not 
obfuscated in the traditional sense]
ok, I apprieciate your comments - its always good to get a experienced
'hackers' view on things. thank you Martin!
cheers,
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php