Re: nesting mySQL queries/while statements
| From: | php3 at developersdesk dot com | Date: | Tue, 09 Jan 2001 02:25:00 +0000 |
| Subject: | Re: nesting mySQL queries/while statements | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-33365@lists.php.net to get a copy of this message | ||
Addressed to: Geoff Coffey <gcoffey@wmotion.com>
php-general@lists.php.net
** Reply to note from Geoff Coffey <gcoffey@wmotion.com> Mon, 08 Jan 2001 17:31:38 -0700
>
> on 1/5/01 9:05 PM, Maurice Rickard at maurice@mauricerickard.com
> wrote:
>
> I don't have much MySQL experience, but probably your problem is
> you're trying to do another query while your result set is open in
> the first query. To do it the way you're doing it, you'll probably
> need a second connection to the database. The outer query uses one
> connection and the inner query uses the second.
>
..snip...
>
> Does that make sense?
>
It makes sense, but it is not true. It is quite legal (at least in
MySQL) to do something like this:
mysql_connect( ... );
mysql_selectdb( ... );
$R1 = mysql_query( "Select something..." );
while( list( $Something, ... ) = mysql_fetch_row( $R1 )) {
$R2 = mysql_query( "Select something_else..." );
...
...
...
mysql_free_result( $R2 );
}
$R1 and $R2 (result sets) do not step on each other, and do not require
a separate connection. Connections are expensive and best avoided
unless actually needed.
The free result is not needed on PHP4 as it does much better at freeing
things you can not access any more, but I believe it is needed with
PHP3 or you end up with a bunch of result sets allocated that you can't
access. They go away when the script ends, but still if you are not
using it any more, you may as well free it.
Rick Widmer
Internet Marketing Specialists
http://www.developersdesk.com