在需要其他任何東西之前,我需要索引中的pdo_connect 在index.php中使用foreach(請參見下文),但是當我在功能「測試」(這是一個單獨的文件),我得到的錯誤:在pdo中的非對象上調用成員函數query()
調用一個成員函數查詢()一個非對象
已經嘗試過在單獨的文件requireing的pdo_connect以及這給同樣的錯誤
我完全新的PDO和初學者編碼器和會很感激,如果有人可以幫助我!
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);
}
功能(testfile.php):
function test($UserName,$PassWord)
{
foreach($DBH->query('SELECT * FROM users') as $row)
{
echo $row['username'].' '.$row['password']; //etc...
}
}
閱讀有關[範圍](http://php.net/manual/en/language.variables.scope.php):
嘗試。 '$ DBH'不是內部'測試訪問()' – kero