Below is my DB connection class. The problem is that when I try to access the CloseConnection function from my code, it gives error: "Unknown MySQL localhost". I use "dbconnection::CloseConnection" through my other code files. It is successfully opening the connection, but giving error in "$conn".
final class dbconnection
{
private $conn;
//Opens connection for a MySQL DB
public static function OpenConnection()
{
require("../Config/dbconfig.php");
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_select_db('MyDB');
}
//Closes connection for a MySQL DB
public static function CloseConnection()
{
mysql_close($conn);
}
}
Below is a method in another PHP file from where I access the above functions:
public static function InsertRecord($inQuery)
{
dbconnection::OpenConnection();
$resultSet = mysql_query($inQuery);
dbconnection::CloseConnection();
return $resultSet;
}
When I remove the line "dbconnection::CloseConnection()", it works fine. I also want to know whether it is a good practice to immediately close the connection as the DB task is finished, or should I keep it open till the use closes the browser?