未定义的变量,但变量在外部文件中定义

时间:2016-07-11 07:44:50

标签: php pdo

我有一个外部的connection.php文件,我将该文件包含在我的fucntions.php中,如require(' ../ admin / includes / connection.php');现在我正在尝试创建一个准备语句,当我添加变量$ smtp我得到一个未定义变量的错误$ smtp为什么这样,因为$ smtp在我的connection.php文件中

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;
    }
}

最重要的是,在正常文件中,当我写$ smtp它工作正常,但当我尝试在函数文件中调用此变量时,我得到了未定义的变量错误

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();

0 个答案:

没有答案