MySqli Database Connection


if(!class_exists('Database')) {
class Database
{
private $db_host = 'localhost';
private $db_user = 'daso_user1';
private $db_pass = 'pwd1';
private $db_name = 'daso_db1';
private $con = '';
public $lastQuery = null;

public function __construct()
{
if($_SERVER['HTTP_HOST'] == 'localhost') {
$this->db_host = 'localhost';
$this->db_user = 'root';
$this->db_pass = '';
$this->db_name = 'db_admin';
}
}


function __destruct()
{
// if($this->con) {
try {
// if(is_resource($this->con) && get_resource_type($this->con) === 'mysql link') {
if(isset($this->con->server_info)) {
/* if server is alive */

// $page_name = basename($_SERVER['PHP_SELF']);
/*
$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https')
. "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
*/
// $page_name = $_SERVER['PHP_SELF'];
// $query = "INSERT IGNORE INTO `log_connection_not_closed`(`page_name`) VALUES ('$page_name')";
// $this->con->query($query);

$this->con->close();
}
// else {
//
// }
}
catch(Exception $e) {
// echo 'Message: ' .$e->getMessage();
}
// }
}

public function connect()
{
$this->con = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
if(mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
die;
}
else {
// $this->con->query("SET NAMES utf8");
                $this->con->set_charset('utf8mb4');
$this->con->query("SET @@session.time_zone = '+05:30'");
                /*
                $this
                ->con
                ->query("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));");
*/
                
                return $this->con;
            }
}
    }
}



Comments

Popular posts from this blog

php - get vimeo video id from url

Hosting a Laravel 11 project on cPanel shared hosting