i have config file that contain a function that connects to database on my server and here is the code
config file
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('USER', 'root');
define('PASS', '');
function communicate($dbname){
try {
$dbh = new PDO('mysql:host=serverip;dbname='.$dbname.'', USER, PASS);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
$errorCode = $e->getCode();
}
}
?>
other page to do some stuff
require('dbs/connfile.php');
if (isset($_GET['id'])) {
$accountname = 'dbname';
communicate($accountname);
$query = "SELECT * FROM datatbl";
$result= $dbh->prepare($query);
$result->execute();
while ($row = $result->fetch()) {
$cmail = $row['email'];
}
}
i got and error that said
Notice: Undefined variable: dbh in /home/moudlepath/page.php
and thats because $dbh
isn't global how can i use that connection file in any page that included into it ?