re-using variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mitesh

    re-using variables

    Hi all,

    I have the following code:

    $req = "SELECT * FROM table1";
    $res = mysql_query($re q);

    if(!$res)
    return;

    while($line = mysql_fetch_arr ay($res))
    {
    .............
    .............
    }

    $req = "SELECT * FROM table2";
    $res = mysql_query($re q);

    if(!$res)
    return;

    while($line = mysql_fetch_arr ay($res))
    {
    .............
    .............
    }

    What I have discovered is re-using the $req variable is fine. While re-
    using the $res produces undefined result (sometimes the second query
    works fine and sometimes it doesn't. When I use different variables
    like $res1 and $res2 the mysql queries always succeed. Is the initial
    approach of re-using variable wrong if variables contain a
    resource(refere nce type)?

    In other words since PHP uses garbage collection re-using the same
    variable can cause problems. Is this true?

  • Ivan Marsh

    #2
    Re: re-using variables

    On Tue, 27 Feb 2007 09:10:28 -0800, Mitesh wrote:
    What I have discovered is re-using the $req variable is fine. While re-
    using the $res produces undefined result (sometimes the second query
    works fine and sometimes it doesn't. When I use different variables like
    $res1 and $res2 the mysql queries always succeed. Is the initial
    approach of re-using variable wrong if variables contain a
    resource(refere nce type)?
    >
    In other words since PHP uses garbage collection re-using the same
    variable can cause problems. Is this true?
    Google: mysql free result

    Comment

    • Mitesh

      #3
      Re: re-using variables


      Ivan Marsh wrote:
      On Tue, 27 Feb 2007 09:10:28 -0800, Mitesh wrote:
      >
      What I have discovered is re-using the $req variable is fine. While re-
      using the $res produces undefined result (sometimes the second query
      works fine and sometimes it doesn't. When I use different variables like
      $res1 and $res2 the mysql queries always succeed. Is the initial
      approach of re-using variable wrong if variables contain a
      resource(refere nce type)?

      In other words since PHP uses garbage collection re-using the same
      variable can cause problems. Is this true?
      >
      Google: mysql free result
      Thanx, but that doesn't address my question. I may have another type
      of resource other than mysql result. So, should I be concerned about
      re-using reference variables in PHP bound to some resource?

      Comment

      • Sjoerd

        #4
        Re: re-using variables

        Mitesh wrote:
        $res = mysql_query($re q);
        .............
        $res = mysql_query($re q);
        In other words since PHP uses garbage collection re-using the same
        variable can cause problems. Is this true?
        Using the same variable twice is fine. Don't bother too much about the
        garbage collector. Since it removes variables when you don't use them
        anymore, it never gets in the way.

        You probably have another problem which causes things to fail sometimes.

        Sjoerd

        Comment

        • Ivan Marsh

          #5
          Re: re-using variables

          On Tue, 27 Feb 2007 09:57:27 -0800, Mitesh wrote:
          >
          Ivan Marsh wrote:
          >On Tue, 27 Feb 2007 09:10:28 -0800, Mitesh wrote:
          >>
          What I have discovered is re-using the $req variable is fine. While re-
          using the $res produces undefined result (sometimes the second query
          works fine and sometimes it doesn't. When I use different variables like
          $res1 and $res2 the mysql queries always succeed. Is the initial
          approach of re-using variable wrong if variables contain a
          resource(refere nce type)?
          >
          In other words since PHP uses garbage collection re-using the same
          variable can cause problems. Is this true?
          >>
          >Google: mysql free result
          >
          Thanx, but that doesn't address my question. I may have another type
          of resource other than mysql result. So, should I be concerned about
          re-using reference variables in PHP bound to some resource?
          I haven't had any trouble with it... but then I tend to destroy/create
          anything I'm going to re-use.

          Comment

          • Tim Streater

            #6
            Re: re-using variables

            In article <1172596225.876 668.42750@k78g2 000cwa.googlegr oups.com>,
            "Mitesh" <oopsbabies@hot mail.comwrote:
            Hi all,
            >
            I have the following code:
            >
            $req = "SELECT * FROM table1";
            $res = mysql_query($re q);
            >
            if(!$res)
            return;
            >
            while($line = mysql_fetch_arr ay($res))
            {
            .............
            .............
            }
            >
            $req = "SELECT * FROM table2";
            $res = mysql_query($re q);
            >
            if(!$res)
            return;
            >
            while($line = mysql_fetch_arr ay($res))
            {
            .............
            .............
            }
            >
            What I have discovered is re-using the $req variable is fine. While re-
            using the $res produces undefined result (sometimes the second query
            works fine and sometimes it doesn't. When I use different variables
            like $res1 and $res2 the mysql queries always succeed. Is the initial
            approach of re-using variable wrong if variables contain a
            resource(refere nce type)?
            >
            In other words since PHP uses garbage collection re-using the same
            variable can cause problems. Is this true?
            Pass. But I systematically do mysql_free_resu lt($res) whether I need to
            or not, not just before a re-use of $res in another query, but also at
            the end of the script. I don't know how it works internally or how much
            one needs to worry about this.

            Comment

            • Ivan Marsh

              #7
              Re: re-using variables

              On Tue, 27 Feb 2007 20:44:47 +0000, Tim Streater wrote:
              In article <1172596225.876 668.42750@k78g2 000cwa.googlegr oups.com>,
              "Mitesh" <oopsbabies@hot mail.comwrote:
              >
              >>
              >What I have discovered is re-using the $req variable is fine. While re-
              >using the $res produces undefined result (sometimes the second query
              >works fine and sometimes it doesn't. When I use different variables
              >like $res1 and $res2 the mysql queries always succeed. Is the initial
              >approach of re-using variable wrong if variables contain a
              >resource(refer ence type)?
              >>
              >In other words since PHP uses garbage collection re-using the same
              >variable can cause problems. Is this true?
              >
              Pass. But I systematically do mysql_free_resu lt($res) whether I need to
              or not, not just before a re-use of $res in another query, but also at
              the end of the script. I don't know how it works internally or how much
              one needs to worry about this.
              The resource gets freed automatically at the end of the script. It's
              always a good idea to free something you're going to re-use in case your
              code isn't working properly you could be left with old data still in the
              resource that's hiding where your real issue is.


              Comment

              • Toby A Inkster

                #8
                Re: re-using variables

                Mitesh wrote:
                What I have discovered is re-using the $req variable is fine. While re-
                using the $res produces undefined result (sometimes the second query
                works fine and sometimes it doesn't. When I use different variables
                like $res1 and $res2 the mysql queries always succeed.
                Well, obviously.

                You have:

                $res = mysql_query("SE LECT * FROM table1");
                ...
                $res = mysql_query("SE LECT * 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.

                --
                Toby A Inkster BSc (Hons) ARCS
                Contact Me ~ http://tobyinkster.co.uk/contact
                Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

                * = I'm getting there!

                Comment

                • Jerry Stuckle

                  #9
                  Re: re-using variables

                  Ivan Marsh wrote:
                  On Tue, 27 Feb 2007 20:44:47 +0000, Tim Streater wrote:
                  >
                  >In article <1172596225.876 668.42750@k78g2 000cwa.googlegr oups.com>,
                  > "Mitesh" <oopsbabies@hot mail.comwrote:
                  >>
                  >>What I have discovered is re-using the $req variable is fine. While re-
                  >>using the $res produces undefined result (sometimes the second query
                  >>works fine and sometimes it doesn't. When I use different variables
                  >>like $res1 and $res2 the mysql queries always succeed. Is the initial
                  >>approach of re-using variable wrong if variables contain a
                  >>resource(refe rence type)?
                  >>>
                  >>In other words since PHP uses garbage collection re-using the same
                  >>variable can cause problems. Is this true?
                  >Pass. But I systematically do mysql_free_resu lt($res) whether I need to
                  >or not, not just before a re-use of $res in another query, but also at
                  >the end of the script. I don't know how it works internally or how much
                  >one needs to worry about this.
                  >
                  The resource gets freed automatically at the end of the script. It's
                  always a good idea to free something you're going to re-use in case your
                  code isn't working properly you could be left with old data still in the
                  resource that's hiding where your real issue is.
                  >
                  >
                  One correction. It gets freed when the garbage collector runs. This is
                  sometime after the script ends, but may not be immediately.

                  I always free my results, close connections, and generally clean up for
                  myself. It's just good programming practice.

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: re-using variables

                    Toby A Inkster wrote:
                    Mitesh wrote:
                    >
                    >What I have discovered is re-using the $req variable is fine. While re-
                    >using the $res produces undefined result (sometimes the second query
                    >works fine and sometimes it doesn't. When I use different variables
                    >like $res1 and $res2 the mysql queries always succeed.
                    >
                    Well, obviously.
                    >
                    You have:
                    >
                    $res = mysql_query("SE LECT * FROM table1");
                    ...
                    $res = mysql_query("SE LECT * 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.

                    But it's still not good programming practice.


                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Michael Fesser

                      #11
                      Re: re-using variables

                      ..oO(Jerry Stuckle)
                      >Toby A Inkster wrote:
                      >
                      >You have:
                      >>
                      > $res = mysql_query("SE LECT * FROM table1");
                      > ...
                      > $res = mysql_query("SE LECT * 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.



                      Micha

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: re-using variables

                        Michael Fesser wrote:
                        .oO(Jerry Stuckle)
                        >
                        >Toby A Inkster wrote:
                        >>
                        >>You have:
                        >>>
                        >> $res = mysql_query("SE LECT * FROM table1");
                        >> ...
                        >> $res = mysql_query("SE LECT * 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.
                        >

                        >
                        Micha
                        We're not talking PDO , Micha.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        • Mitesh

                          #13
                          Re: re-using variables


                          Jerry Stuckle wrote:
                          Michael Fesser wrote:
                          .oO(Jerry Stuckle)
                          Toby A Inkster wrote:
                          >
                          >You have:
                          >>
                          > $res = mysql_query("SE LECT * FROM table1");
                          > ...
                          > $res = mysql_query("SE LECT * 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.



                          Micha
                          >
                          We're not talking PDO , Micha.
                          >
                          --
                          =============== ===
                          Remove the "x" from my email address
                          Jerry Stuckle
                          JDS Computer Training Corp.
                          jstucklex@attgl obal.net
                          =============== ===
                          Ok if we need to call, mysql_free_resu lt 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)

                          Comment

                          • Tim Streater

                            #14
                            Re: re-using variables

                            In article <1172668483.913 253.134510@h3g2 000cwc.googlegr oups.com>,
                            "Mitesh" <oopsbabies@hot mail.comwrote:
                            Jerry Stuckle wrote:
                            Michael Fesser wrote:
                            .oO(Jerry Stuckle)
                            >
                            >Toby A Inkster wrote:
                            >>
                            >>You have:
                            >>>
                            >> $res = mysql_query("SE LECT * FROM table1");
                            >> ...
                            >> $res = mysql_query("SE LECT * 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.
                            >

                            >
                            Micha
                            We're not talking PDO , Micha.

                            --
                            =============== ===
                            Remove the "x" from my email address
                            Jerry Stuckle
                            JDS Computer Training Corp.
                            jstucklex@attgl obal.net
                            =============== ===
                            >
                            Ok if we need to call, mysql_free_resu lt 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_resu lt, 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_resu lt 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

                            Comment

                            • Mitesh

                              #15
                              Re: re-using variables


                              Tim Streater wrote:
                              In article <1172668483.913 253.134510@h3g2 000cwc.googlegr oups.com>,
                              "Mitesh" <oopsbabies@hot mail.comwrote:
                              >
                              Jerry Stuckle wrote:
                              Michael Fesser wrote:
                              .oO(Jerry Stuckle)

                              Toby A Inkster wrote:
                              >
                              >You have:
                              >>
                              > $res = mysql_query("SE LECT * FROM table1");
                              > ...
                              > $res = mysql_query("SE LECT * 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.



                              Micha
                              >
                              We're not talking PDO , Micha.
                              >
                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===
                              Ok if we need to call, mysql_free_resu lt 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_resu lt, 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_resu lt 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_arr ay 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_resu lt. The product is
                              already shipped and it seems to be working.

                              Comment

                              Working...