简体   繁体   English

PHP PDO配置文件包括 - global

[英]PHP PDO config file include - global

I am making a transfer from using mysql query lines to PDO for a current project and i have an issue. 我正在使用mysql查询行转移到当前项目的PDO,我有一个问题。 For this task i am not allowed to use any classes (stupid restriction if you ask me) 对于这个任务,我不允许使用任何课程(如果你问我,愚蠢的限制)

Basically i was getting a non object error because my main php file could not see the set variable $DBH. 基本上我得到一个非对象错误,因为我的主要php文件无法看到设置变量$ DBH。 I solved this problem by setting each function with a $DBH global; 我通过设置$ DBH全局函数来解决这个问题; so it could be used, however ive been told this is bad coding practice. 所以它可以使用,但是我被告知这是糟糕的编码实践。 Is this the case? 是这样的吗? and if so how can i make my function see my config variable. 如果是这样,我怎么能让我的函数看到我的配置变量。

Config.php config.php文件

try 
{
    $DBH = new PDO("mysql:host=host;dbname=db", "username", "Password");
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
catch (PDOException $e){
    echo $e->getMessage();
}

a php file 一个php文件

function concName($concID)
{   
        global $DBH; //THIS is the area that im told is bad practice - can this be eliminated?
        $stmt = $DBH->prepare("SELECT CONCAT(`Firstname`, ' ', `Surname`) AS 'Membername'     FROM `members` WHERE `MemberID`= :MemberID");
        $stmt->bindValue(":MemberID",$concID);
        $stmt->execute();

        while($row = $stmt->fetch())
        {
            return $row['Membername'];      
        }

}

Just pass $DBH as a parameter to any function that needs it: 只需将$DBH作为参数传递给任何需要它的函数:

function concName($concID, $DBH)
{   
        $stmt = $DBH->prepare("SELECT CONCAT(`Firstname`, ' ', `Surname`) AS 'Membername'     FROM `members` WHERE `MemberID`= :MemberID");
        $stmt->bindValue(":MemberID",$concID);
        $stmt->execute();

        while($row = $stmt->fetch())
        {
            return $row['Membername'];      
        }

}

Rather than the global keyword, you can also access it from the $GLOBALS[] array, which is more explicit about the variable's origins when used in the function. 您还可以从$GLOBALS[]数组访问它,而不是global关键字,该数组在函数中使用时更明确地了解变量的来源。 Passing a parameter is still preferable to this though. 但是传递参数仍然比这更好。

function concName($concID)
{   
        // Better than `global` keyword, use `$GLOBALS['DBH']` every time you access it in outside global scope
        // Still not preferred to passing a parameter though.
        $stmt = $GLOBALS['DBH']->prepare("SELECT CONCAT(`Firstname`, ' ', `Surname`) AS 'Membername'     FROM `members` WHERE `MemberID`= :MemberID");
        $stmt->bindValue(":MemberID",$concID);
        $stmt->execute();

        while($row = $stmt->fetch())
        {
            return $row['Membername'];      
        }

}

If you have multiple globals defined in your configuration file, you can wrap them all in an array which you pass into functions needing them. 如果在配置文件中定义了多个全局变量,则可以将它们全部包装在一个数组中,然后传递给需要它们的函数。 That wraps them tidily into a package of config options made available to any function that needs them. 这将它们整齐地包装到一个配置选项包中,这些配置选项可用于任何需要它们的功能。

config.php config.php文件

// Global array of config options
$config = array();

// various options
$config['option1'] = 'option 1';
$config['option2'] = 12345;

try 
{
    $config['DBH'] = new PDO("mysql:host=host;dbname=db", "username", "Password");
    $config['DBH']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
catch (PDOException $e){
    echo $e->getMessage();
}

Then pass $config to function calls 然后将$config传递给函数调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM