PHP Config / mysql Connection

Php configuration / connection file


By using it we can do a better coding.
// config.php

<?php
//error_reporting(0);
if(!session_id())
{
session_start();
}
//localhost connection
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'dbname');
define('ROOT', 'http://localhost/phpconnection');
?>
<?php
class connectionClass
{
public $db;
public $resquest;
public $server;
public $root;
function __construct()
{
$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
mysqli_set_charset($this->db,"utf8");
if($this->db->connect_errno > 0)
{
    die('Unable to connect to database [' . $db->connect_error . ']');
}
}
}
?>
_____________________________________
Use of this config file
// select.php file
<?php 
include 'config.php';
$newobj = new connectionClass();
$db = $newobj ->db;
$Totaltimecurrent = "SELECT TIMESTAMPDIFF(SECOND, '".$StartDatecurrent."', '".$EndDatecurrent."') as myTime";
$resultcurrentdaytime = $db->query($Totaltimecurrent);
$datacurrentdaytime = $resultcurrentdaytime->fetch_assoc();

$totalDurationcurrent =  gmdate("H:i:s", $datacurrentdaytime['myTime']);
echo $totalDurationcurrent ;
?>

Comments