473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)
Jul 17 '05 #1
5 2840
"ahevans" <ah*****@gmail. com> wrote in message
news:d5******** *************** **@posting.goog le.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++.
Jul 17 '05 #2
"Chung Leong" <ch***********@ hotmail.com> wrote in message news:<gK******* *************@c omcast.com>...
"ahevans" <ah*****@gmail. com> wrote in message
news:d5******** *************** **@posting.goog le.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?
Jul 17 '05 #3
ahevans wrote:
"Chung Leong" <ch***********@ hotmail.com> wrote in message news:<gK******* *************@c omcast.com>...
"ahevans" <ah*****@gmail. com> wrote in message
news:d5****** *************** ****@posting.go ogle.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, <de*****@cauce. org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #4
.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
Jul 17 '05 #5
> 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?

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2124
by: lawrence | last post by:
Given a bunch of mixes variables, I'm in a situation where I can't know what type they are. Can I use unset to kill an object? unset($object); Can I use unset to kill a pointer to an open file? unset($fp); Can I use unset to kill a pointer to a database return? unset($mySqlResult);
21
3381
by: steve | last post by:
Dont’ make my mistake. It is costly. Say you have defined a variable $var in your main script. Now in a function you access it using: global $var; But you want to set it to null inside the function. DO: $var = null; DONT DO: unset($var);
3
4107
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which will discard the variable alltogether... if ($categoryid == "all") { $sql = "SELECT * FROM products where shopinspection=$shopinspection"; unset($HTTP_POST_VARS); unset($HTTP_POST_VARS);
1
1884
by: frizzle | last post by:
Hi group, Why won't $new_var be unset in the following function? Am i missing out something? Greetings Frizzle. *************************************
7
1851
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which will discard the variable alltogether... if ($categoryid == "all") { $sql = "SELECT * FROM products where shopinspection=$shopinspection"; unset($HTTP_POST_VARS); unset($HTTP_POST_VARS);
7
5235
by: lievendp | last post by:
Just wondering, Is it possible for an instance of a class to unset itself? can I do "unset($this)" or "unset(&$this)" regards, Lieven http://eye.cc php newsgroups
5
2292
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray) || @sizeof($leaseObj->errorArray) == 0)) { print_r(array_keys($_POST)); @reset($_POST); $tempPost = $_POST; foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0) array_remove($tempPost, $tempPost);
1
4505
by: Nico | last post by:
Hi all, I've created the following code: <?php session_start(); ?> <FORM METHOD="POST" ACTION="prova.php"> Add <b>Combination</b><br><br>
0
1413
Airslash
by: Airslash | last post by:
Hello, I've written a small function to delete a variable from a class' internal array. The variables on their own are custom class objects, and I'm a bit confused about the whole pass by reference thing. First I'll show you the code: # removes a parameter from the array. # variables are passed as reference to prevent overhead.
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.