when to use mysql_close and unset()

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

    #1

    when to use mysql_close and unset()

    Howdy all,

    I'm progressing with PHP but I have 2 questions that are kinda bugging
    me.

    Say for example, I have a php page similar to this....

    if .....

    if........

    elseif......

    else........

    else ....

    etc. where I have if/else statements nested in other ones.

    Now I guess it's good practice to unset created variables within each
    if or else section as a) it's less work and b) secure (correct me if
    I'm wrong).

    IF I'm using mysql_close, do I just need to open one connection at the
    top of the page and close it at the bottom, as I think I understand
    that php will only run queries within if/else which evaluate to true
    (and so all can use one connection), and then I can close the
    connection at the bottom of the page as php will go through all the
    code?

    I hope that made sense :o)
  • Chung Leong

    #2
    Re: when to use mysql_close and unset()

    "ahevans" <ahevans@gmail. com> wrote in message
    news:d589c3f.05 02190717.69ea18 23@posting.goog le.com...[color=blue]
    > Now I guess it's good practice to unset created variables within each
    > if or else section as a) it's less work and b) secure (correct me if
    > I'm wrong).[/color]

    Never heard of that one, although I can see how some people would do it to
    simulate the scoping rule in C++.


    Comment

    • ahevans

      #3
      Re: when to use mysql_close and unset()

      "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<gKKdnX7sD IFJqYXfRVn-2A@comcast.com> ...[color=blue]
      > "ahevans" <ahevans@gmail. com> wrote in message
      > news:d589c3f.05 02190717.69ea18 23@posting.goog le.com...[color=green]
      > > Now I guess it's good practice to unset created variables within each
      > > if or else section as a) it's less work and b) secure (correct me if
      > > I'm wrong).[/color]
      >
      > Never heard of that one, although I can see how some people would do it to
      > simulate the scoping rule in C++.[/color]

      Is it common practice to unset all variables or are they unset when a
      browser leaves a page?

      Comment

      • Jan Pieter Kunst

        #4
        Re: when to use mysql_close and unset()

        ahevans wrote:[color=blue]
        > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<gKKdnX7sD IFJqYXfRVn-2A@comcast.com> ...
        >[color=green]
        >>"ahevans" <ahevans@gmail. com> wrote in message
        >>news:d589c3f. 0502190717.69ea 1823@posting.go ogle.com...
        >>[color=darkred]
        >>>Now I guess it's good practice to unset created variables within each
        >>>if or else section as a) it's less work and b) secure (correct me if
        >>>I'm wrong).[/color]
        >>
        >>Never heard of that one, although I can see how some people would do it to
        >>simulate the scoping rule in C++.[/color]
        >
        >
        > Is it common practice to unset all variables or are they unset when a
        > browser leaves a page?[/color]

        In PHP, everything ends when the script has completed. So by the time
        the page has finished rendering in the user's browser, all variables are
        already unset. (Apart from session variables which may have been saved
        in a file on the server somewhere.)

        JP

        --
        Sorry, <devnull@cauce. org> is a spam trap.
        Real e-mail address unavailable. 5000+ spams per month.

        Comment

        • Michael Fesser

          #5
          Re: when to use mysql_close and unset()

          .oO(ahevans)
          [color=blue]
          >Is it common practice to unset all variables[/color]

          I don't think so.
          [color=blue]
          >or are they unset when a
          >browser leaves a page?[/color]

          PHP cleans up when the script is finished. The only things I release
          manually are resources (DB connections etc.), which can be easily done
          in destructors when using OOP.

          Micha

          Comment

          • gildir

            #6
            Re: when to use mysql_close and unset()

            > IF I'm using mysql_close, do I just need to open one connection at
            the[color=blue]
            > top of the page and close it at the bottom[/color]

            Use only one connection. Connect at the top (before your if statements)
            and close at the bottom (after all the if statements). You should
            always do it this way while you are a beginner to "scope issues".
            [color=blue]
            > as I think I understand
            > that php will only run queries within if/else which evaluate to true[/color]

            Becareful here.

            //example 1
            if(true) echo "Hello";
            elseif(true) echo " Bob";
            else echo " Peterson";

            //example 2
            if(false) echo "Well! ";
            elseif(true) echo "Hello";
            elseif(true) echo " Bob";
            else echo " Peterson";

            Both examples will only print "Hello" not "Hello Bob" or "Hello Bob
            Peterson". Any elseif/else statement below another true statement will
            not execute.

            //example 3
            if(false) echo "Hello";
            elseif(true)
            {
            echo "Bob";

            if(true) echo " Peterson";
            elseif(false) echo " Smith";
            else echo " Lesterson";
            }

            This will print "Bob Peterson".
            [color=blue]
            > Now I guess it's good practice to unset created variables within each
            > if or else section as a) it's less work and b) secure (correct me if
            > I'm wrong).[/color]

            Never heard this either, but I am more curious about (a). How is it
            less work to unset variables?

            Comment

            Working...