You are here: Re: re-using variables « PHP Programming Language « IT news, forums, messages
Re: re-using variables

Posted by Mitesh on 02/28/07 17:12

Mitesh wrote:
> Tim Streater wrote:
> > In article <1172668483.913253.134510@h3g2000cwc.googlegroups.com>,
> > "Mitesh" <oopsbabies@hotmail.com> wrote:
> >
> > > Jerry Stuckle wrote:
> > > > Michael Fesser wrote:
> > > > > .oO(Jerry Stuckle)
> > > > >
> > > > >> Toby A Inkster wrote:
> > > > >>
> > > > >>> You have:
> > > > >>>
> > > > >>> $res = mysql_query("SELECT * FROM table1");
> > > > >>> ...
> > > > >>> $res = mysql_query("SELECT * FROM table2");
> > > > >>> if(!$res)
> > > > >>> return;
> > > > >>>
> > > > >>> $res is still going to be set as a result of your first query.
> > > > >>>
> > > > >>> Use unset($res) between each query.
> > > > >> No, Toby.
> > > > >>
> > > > >> $res will have the results of the second query. The resource returned
> > > > >> by the second query (or false) will overwrite what was in $res.
> > > > >
> > > > > Try that with PDO and it will crash most likely. Overwriting $res will
> > > > > not necessarily free the previous result set, which might then lead to
> > > > > MySQL complaining about an unbuffered query or something like that.
> > > > >
> > > > > | You cannot use the same variable for a PDOStatement object twice. As
> > > > > | others have pointed out it works when you set this variable to null in
> > > > > | between.
> > > > >
> > > > > http://bugs.php.net/bug.php?id=35793
> > > > >
> > > > > Micha
> > > >
> > > > We're not talking PDO , Micha.
> > > >
> > > > --
> > > > ==================
> > > > Remove the "x" from my email address
> > > > Jerry Stuckle
> > > > JDS Computer Training Corp.
> > > > jstucklex@attglobal.net
> > > > ==================
> > >
> > > Ok if we need to call, mysql_free_result for mysql resources doesn't
> > > that mean every other type of resources bound this way to a variable
> > > must have a freeing function that has to be called before the variable
> > > can be re-used? So doesn't it coincide with what I am saying that
> > > variables bound to resources when re-used may cause problems. (and
> > > according to the others post the variables can be re-used if the
> > > previously allocated resource is freed)
> >
> > My understanding was that, if you re-use $res without a call to
> > mysql_free_result, it re-uses what $res is, which is a pointer to a
> > result set, but doing that does *not* free up the space used by the
> > result set. At least that's how it used to be. I don't know if they have
> > improved the internals such that mysql_free_result is now a dummy call,
> > or whether that is inherently impossible because mysql itself holds the
> > result set.
> >
> > Perhaps a guru can enlighten us.
> >
> > -- tim
>
> My concern is not if the previous resource is deallocated or not. I
> don't have a huge result set returned. It will eventually be freed.
>
> What I am really afraid is the working of my current resource pointed
> to by $res which I have experienced to be not working occasionally as
> I stated that my second fetch array function suceeds some times and
> fails other times.
>
> Can the following happen?
>
> When I overwrite $res variable (whatever the variable maybe a pointer
> or a reference). Suppose the garbage collector kicks in and finds that
> the previous resource isn't owned by any variable and tries to free
> it. When it tries to free, it accesses the $res variable. But now the
> $res variable owns another resource i.e. my second mysql result set
> and wrongly frees the second resource hence my second
> mysql_fetch_array fails.
>
> I am sure that I have read that in PHP variables that contain
> resources are reference variables (similar to pointers) unlike other
> variables like the $req variable storing a string. We can indded make
> a variable act like a reference using an ampersand i think.
>
> Yes it seems we need a guru here.
>
> So my would be question to our guru is can we safely re-use variables
> that are references?
>
> Right now I resolved the situation using two different variable names
> before I got the suggestion on using mysql_free_result. The product is
> already shipped and it seems to be working.

Sorry some corrections on my part:

I had in my post said that my second query sometimes succeeds and
sometimes doesn't. The query succeeds in both occasions however
mysql_fetch_array sometimes succeeds and sometimes fails.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация