1
我具有包括另一文件中的文件:致命錯誤:調用一個成員函數準備()非對象與PDO
的index.php是一個動態的模板
<?php
$db = new PDO("mysql:host=localhost;dbname=db", $DB_USER, $DB_PASSWORD);
include(functions.php);
?>
功能。 PHP
<?php
$preparedStatement = $db->prepare("SELECT id FROM table LIMIT 1");
$preparedStatement->execute();
$firstId = $preparedStatement->fetchAll();
?>
的functions.php執行時,我得到:
Fatal error: Call to a member function prepare() on a non-object
在行
$preparedStatement = $db->prepare("SELECT id FROM table LIMIT 1");
什麼是錯的代碼,使得它拋出的錯誤?
編輯:錯誤產生於我直接連接的functions.php文件,它沒有出現,當它包含在index.php中時,我遇到的問題是內容沒有加載到後來的內容中,這是由我造成的忘記在後面的包含文件中正確地處理數組。所選答案對我提出的問題是正確的。
反之亦然
最佳實踐。如果我這樣做,我有包括對方的文件,他們只是不停地裝載。 – valon
我需要index.php來加載functions.php,index.php是一個動態模板。 – valon