I looked around, but couldn't find an answer to this anywhere.
If I'm creating a class and I need to query MySQL in different functions of the class, is there a way to require a conn.php file only once to be used for all functions?
So far I'm requiring a connection file in each of the functions, but it seems resource expensive to require a connection multiple times throughout a class.
...EDIT ... So like this maybe?
private $conn;
function __construct() {
$this->conn = new mysqli("", "", "", "");
}
function insert() {
$sql = "";
$query = $this->conn->prepare($sql);
$query->bind_param();
$query->execute();
$query->free_result();
$query->close();
}
function update() {
$sql = "";
$query = $this->conn->prepare($sql);
$query->bind_param();
$query->execute();
$query->free_result();
$query->close();
}
This is working really well. Is there any drawback to this? Is there a more professional way to do this?