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.