i just started oop and now, even very simple things seem confusing to me! this is my class for mysqli connection:
class DB{
private $con;
public function __construct(){
$this->con=new mysqli('localhost','root','','dbName');
if(!$this->con){
echo '<b>There was a problem connecting to database! </b><br />errno: '.$con->connect_errno;
exit;
}
$this->con->set_charset("utf8");
}
public function query($query){
return $this->con->query($query);
}
}
let's say i'm gonna use that like this:
$a=mysqli_real_escape_string($_POST['a']);
$b=mysqli_real_escape_string($_POST['b']);
$query='SELECT `name` FROM `someTable` WHERE `type`={'.$a.'}';
$query2='SELECT `name` FROM `someTable` WHERE `type`={'.$b.'}';
$DB = NEW DB;
$test=$DB->query($query);
$DB2 = NEW DB;
$test2=$DB2->query($query2);
i just created 2 objects from class DB. does it mean there is a new mysql connection for each object? if yes, how can i avoid it?
i know i can use mysqli_close() in __destruct() function but i read somewhere (probably in this very site :) ) that creating and destroying several mysql connections isn't good.
what should i do? p.s: beside, just as i said i'm new in oop (and mysqli as well) so if there is any comment about my class (for example did i set the charset at the right place?) i'll be honored.