在pdo中的非对象上调用成员函数query()

I require the pdo_connect in the index before requiring anything else Using the foreach(see below) in the index.php works but when I use it in the function 'test' (which is a seperate file) I get the error:

Call to a member function query() on a non-object

Already tried requireing the pdo_connect in the seperate file as well which gives the same error

I'm completely new to PDO and a beginner coder and would really appreciate it if someone can help me out!

pdo_connect.php:

        $host= 'localhost';
        $dbname= 'secret';
        $user= 'secret';
        $pass= 'secret';
        $input = 'secret';

        try 
        {  
          $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
          $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        }  
        catch(PDOException $e) 
        {
            file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);  
        } 

Function (testfile.php):

function test($UserName,$PassWord)
{

    foreach($DBH->query('SELECT * FROM users') as $row) 
    {
        echo $row['username'].' '.$row['password']; //etc...
    }
}

You have to pass your $DBH as param to test function, $DBH is not visible inside your test function.

Try:

function test($DBH,$UserName,$PassWord)
{

    foreach($DBH->query('SELECT * FROM users') as $row) 
    {
        echo $row['username'].' '.$row['password']; //etc...
    }
}