I have an external connection.php file and I am including that file in my fucntions.php like require('../admin/includes/connection.php'); now I am trying to create a prepare statement when i add variable $smtp i got an error of undefined variable $smtp why is that so as $smtp is in my connection.php file
connection.php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "ystore";
try {
$smtp = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
} catch(PDOException $e) {
die("Error connecting to database :" . $e->getMessage());
}
Functions.php
require('../admin/includes/connection.php');
class Settings {
public function GetData($table) {
$sql = $smtp->prepare('SELECT * FROM '.$table);
$sql->execute();
$data = $sql->fetch(PDO::FETCH_ASSOC);
return $data;
}
}
The big thing is that in normal files when i write $smtp it is working fine but when i try to call this variable in functions file i got undefined variable error
admin.php
require_once("includes/connection.php");
require_once("includes/functions.php");
//Working fine here
$query = $smtp->prepare("SELECT * FROM admin");
/* execute query */
$query->execute();