How can I make a mysql connection inside a function if my mysql connect is in another include file?

Here is the mysql connect include file.

DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'adfg');

$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$dbc) {
    trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
}

Accepted Answer

File: DB.php

DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'adfg');

function getConnection() {
  $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  if (!$dbc) {
    trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
  } else {
    return $dbc;
  }
}

Other file:

require_once '/path/to/DB.php';

$connection = getConnection();
var_dump($connection->host_info);
Written by powtac
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki