Page 1 of 1
Ensure DB Connection
Posted: Fri Jun 02, 2006 11:01 am
by r_barlow
Hey,
I have a page I'm including in several others that lists information from MySQL. Since all of the pages that will have this page included will be querying information as well, I don't want to bother opening a new connection to the database.
The problem is I want to test whether the connection is open or not and if not, then include the code that is needed (I have these in separate php pages). Right now the code I have is:
Code: Select all
if ($conn == null)
{
include ("dbconfig.php");
include ("dbconnect.php");
}
Where $conn is the connection used in dbconfig.php. This works however I get the following message outputed:
Notice: Undefined variable: conn in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\scorpion\productlinks.php on line 8
Is there a better way to do this?
Posted: Fri Jun 02, 2006 11:08 am
by PrObLeM
why not do this?
Code: Select all
include ("dbconfig.php");
if ($conn == null) {
include ("dbconnect.php");
}
Posted: Fri Jun 02, 2006 11:23 am
by MrPotatoes
that's because you are using $conn before giving it a value.
in C/C++ that would be a serious logic error later down the road. and no one would be happy with you if they found that was the problem.
i love PHP for letting me be sooooooooooooooooooooooooooooooooooooooo lazy as i program lamo
Re: Ensure DB Connection
Posted: Fri Jun 02, 2006 11:24 am
by RobertGonzalez
r_barlow wrote:
Code: Select all
<?php
if ($conn == null)
{
include ("dbconfig.php");
include ("dbconnect.php");
}
?>
Where $conn is the connection used in dbconfig.php. This works however I get the following message outputed:
Notice: Undefined variable: conn in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\scorpion\productlinks.php on line 8
You're getting the notice becuase you are trying to compare a value to a variable that has not been initialized yet. To get rid of the notice, check first if the variable is set, then check it's value...
Code: Select all
<?php
if (isset($conn) && $conn == null)
{
include ("dbconfig.php");
include ("dbconnect.php");
}
?>
This check will work for you, but as a suggestion, if you are going to be querying regularly, why not open the connection for everypage by way of a common include. Then you don't have to worry about checking to see if it is open. My usually process is to open my db connection by way of an include, that is included into a common file that is included into every page. I close the connection in the footer page include so every page load opens the connection and closes the connection at the end.
Please note, every one of my pages has information coming from the database, so I need the connection literally on every page, but it just makes sense to have it available to you when you need it instead of checking to see if it is already there or not. This is just an opinion.
Posted: Fri Jun 02, 2006 1:06 pm
by r_barlow
Thanks a lot for the help.
Yeh I'd do it that way but I don't know if all of my pages are going to be querying information. I just want to double check to make sure everythings fine before querying on this page instead of relying on the page that is including it.
Everything works beautifully now though!!