I'm creating a project where multiple class files exist, so it's desirable to define the DB config once and 'include' that file where ever it's required. However I'm unable to do so. The code is mentioned Below:
<?php
include 'backend_connection.php'; //does not work
class test {
public function __construct()
{
include 'backend_connection.php'; //does not work
}
function getMyOpenHIssueCount($id) {
include 'backend_connection.php'; //here it works only
$stmt = mysqli_prepare($con,"SELECT `name` FROM `emp` where empid=?");
mysqli_stmt_bind_param($stmt,"i",$id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $title);
mysqli_stmt_fetch($stmt);
echo 'Title '.$title;
if (!isset($title))
echo 'No result found';
mysqli_stmt_close($stmt);
}
}
?>
As I plan to have each class to be in separate file, it's desirable to 'include' the backend_connection.php.
How to do so ?
Below is the solution, hope it helps someone someday
<?php
class test {
public function __construct()
{
include 'backend_connection.php';
$con=mysqli_connect($dba_host,$dba_name,$dba_pass,$dba_db) or die('db Connection Refused !');
$this->con=$con;
}
public function getMyOpenHIssueCount($id) {
$stmt = mysqli_prepare($this->con,"SELECT `name` FROM `emp` where empid=?");
mysqli_stmt_bind_param($stmt,"i",$id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $title);
mysqli_stmt_fetch($stmt);
echo 'Title '.$title;
if (!isset($title))
echo 'No result found';
mysqli_stmt_close($stmt);
}
}
?>