Ensure DB Connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
r_barlow
Forum Newbie
Posts: 17
Joined: Mon May 29, 2006 3:13 pm

Ensure DB Connection

Post 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?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

why not do this?

Code: Select all

include ("dbconfig.php");
if ($conn == null) {
   include ("dbconnect.php");
}
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Ensure DB Connection

Post 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.
r_barlow
Forum Newbie
Posts: 17
Joined: Mon May 29, 2006 3:13 pm

Post 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!!
Post Reply