<?phpclassDBConnect {public$connection=false;public$result=false;privatefunctionfreeResult(){if($this->result!=false){mysqli_free_result($this->result);}$this->result=fals...
class DBConnect{
public $connection = false;
public $result=false;
private function freeResult(){
if ($this->result != false){
mysqli_free_result($this->result);
}
$this->result = false;
}
function doquery($sql){
$this->freeResult();
$this->result=mysqli_query($this->connection, $sql);
return $this->result;
}
function __construct(){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mytest";
$this->connection = new mysqli($servername, $username, $password, $dbname);
if ($this->connection->connect_error) {
die("Connection failed: " . $this->connection->connect_error);
}
}
function __destruct(){
$this->freeResult();
if ($this->connection != false){
$this->connection->close();
}
}
}
$myConnection = new DBConnect();
?>
展开