I am connecting to a database using a constructor and disconnecting with a destructor. All I want to know is: is it more efficient when I call this from the constructor? What about the destructor? Is it safe or good coding to connect to a database with a prepared statement?
How do I create a method for insertion?
public function __construct($host, $username, $password, $dbname, $tablename="")
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
$this->tablename = $tablename;
$this->con = mysqli_connect($host, $username, $password, $dbname); //connecting...
if(mysqli_connect_errno($this->con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Connection Established<br>";
}
}
public function __destruct()
{
if(mysqli_close($this->con))
{
echo "<br>Connection Terminated";
}
}