I'm not sure whether I'm currently doing this correctly - within the site that I've built, my configuration file is included into the top of every page, within that file I have a string to open my DB as follows...
if (!$conn = mysql_connect($TheDBHostName, $TheDBUsername, $TheDBPassword)) {
header("Location: offline.php?!=GeneralDBConnectionFailure");
exit;
} else {
$db = mysql_select_db($TheDBDatabaseName,$conn);
}
Then, whenever in a page I require a Recordset, I'm doing something like this...
$sql = "SELECT MemberFirstName, MemberLastName FROM tbl_members_main WHERE MemberID = " . $TheMemberIDToAdd;
$RS_GrabNameOfNewFriend_Query = mysql_query($sql, $conn) or header("Location: offline.php?!=DatabaseTableUnavailable");
$RS_GrabNameOfNewFriend = mysql_fetch_assoc($RS_GrabNameOfNewFriend_Query);
My question is should I close recordsets, OR perhaps even the DB connection? Or am I doing this right by having the opening of the DB connection on every page? This is the way I've always worked in PHP (being a relative newbie), I'm really seeking clarification that I'm right, or that there's a better way?
Also, should I be closing recordsets after using them, at the base of each page? Or are they cleansed automatically?
My host is very stringent on PHP memory usage and currently I'm always going over quota (although its more likely my SMF forum that I have running on the webspace).