I'm trying to create my db connection this way. In Functions_BD I would like to create connection to BD and define all my functions (Login, Insert, Selects...). My class looks like this:
class functions_BD {
private $mysqli;
function __construct() {
require_once 'config.php';
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
}
In 'config.php' I've my DB configuration. Then I have some public functions inside "Functions_BD" like this one
public function login($user,$passw){
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
if (mysqli_connect_errno()) {
return 2;
}
$stmt=$mysqli->prepare("SELECT COUNT(*) FROM users WHERE user= ? AND passw= ? ");
$stmt->bind_param('ss', $user,$passw);
$stmt->execute();
$result = $stmt->fetch();
if($result>0){
return 1;
}else{
return 0;
}
}
If I define $mysqli in each function the code works fine, my question is: is there any way of defining $mysqli only once and using it in all my functions? (Like I tried in the constructor) Or I may define it again every time I do a query?