我正在使用构造函数连接到数据库并与析构函数断开连接。我想知道的是:当我从构造函数中调用它时效率更高吗?析构函数呢?使用准备好的语句连接到数据库是安全的还是好的编码?
如何创建插入方法?
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";
}
}