Skip to content

Instantly share code, notes, and snippets.

@pkavoo
Created May 18, 2019 09:08
Show Gist options
  • Save pkavoo/41a3bf4a3a6668ee2c3657fdf02fc718 to your computer and use it in GitHub Desktop.
Save pkavoo/41a3bf4a3a6668ee2c3657fdf02fc718 to your computer and use it in GitHub Desktop.
<?php
//Class DbConnect
class DbConnect
{
//Variable to store database link
private $con;
//Class constructor
function __construct()
{
}
//This method will connect to the database
function connect()
{
//Including the constants.php file to get the database constants
include_once dirname(__FILE__) . '/Constants.php';
//connecting to mysql database
$this->con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//finally returning the connection link
return $this->con;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment