when to use mysql_close and unset()

This is a discussion on when to use mysql_close and unset() within the PHP Language forums, part of the PHP Programming Forums category; Howdy all, I'm progressing with PHP but I have 2 questions that are kinda bugging me. Say for example, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-19-2005
ahevans
 
Posts: n/a
Default 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)
Reply With Quote
  #2 (permalink)  
Old 02-20-2005
Chung Leong
 
Posts: n/a
Default Re: when to use mysql_close and unset()

"ahevans" <ahevans@gmail.com> wrote in message
news:d589c3f.0502190717.69ea1823@posting.google.co m...
> 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).


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


Reply With Quote
  #3 (permalink)  
Old 02-20-2005
ahevans
 
Posts: n/a
Default Re: when to use mysql_close and unset()

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message news:<gKKdnX7sDIFJqYXfRVn-2A@comcast.com>...
> "ahevans" <ahevans@gmail.com> wrote in message
> news:d589c3f.0502190717.69ea1823@posting.google.co m...
> > 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).

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


Is it common practice to unset all variables or are they unset when a
browser leaves a page?
Reply With Quote
  #4 (permalink)  
Old 02-20-2005
Jan Pieter Kunst
 
Posts: n/a
Default Re: when to use mysql_close and unset()

ahevans wrote:
> "Chung Leong" <chernyshevsky@hotmail.com> wrote in message news:<gKKdnX7sDIFJqYXfRVn-2A@comcast.com>...
>
>>"ahevans" <ahevans@gmail.com> wrote in message
>>news:d589c3f.0502190717.69ea1823@posting.google. com...
>>
>>>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).

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

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


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.
Reply With Quote
  #5 (permalink)  
Old 02-20-2005
Michael Fesser
 
Posts: n/a
Default Re: when to use mysql_close and unset()

.oO(ahevans)

>Is it common practice to unset all variables


I don't think so.

>or are they unset when a
>browser leaves a page?


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
Reply With Quote
  #6 (permalink)  
Old 02-21-2005
gildir
 
Posts: n/a
Default Re: when to use mysql_close and unset()

> 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


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".

> as I think I understand
> that php will only run queries within if/else which evaluate to true


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".

> 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).


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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0