所以我有2個文件,一個是mainvariables.php & load_more.php。 load_more.php是一個php文件,因爲我對它做了一個ajax調用。 mainvariables.php文件有1個函數。我如何包含一個php文件並獲取另一個php文件中的函數值?
這很奇怪,因爲在我的header.php文件中包含mainvariables.php並獲取返回的值,但是當我使用load_more.php文件嘗試它時,它不起作用。
mainvariables.php
<?php
function mainVariables(){
global $pdo;
//getting school id for switch statment
$schoolId = (isset($_GET['id']) ? $_GET['id']:NULL);
if ($schoolId) {
$sql = 'SELECT * FROM schools WHERE id = :id';
$query = $pdo->prepare($sql);
$query->execute(array(':id' => $schoolId));
}else {
echo "Not Working";
}
//all values of current school
return $query->fetchAll(PDO::FETCH_ASSOC);
}
?>
load_more.php
<?php
// including the config file
require('config.php');
//pdo connct for config.php file
$pdo = connect();
//Include main variables function
include('php/mainvariables.php');
//return of main variables function
$specificSchool = mainVariables();
//School Variables
$shcoolOfficialId = $specificSchool[0]["id"];
switch ($shcoolOfficialId) {
case 1:
echo "yes";
break;
case 2:
echo "no";
break;
default:
echo "There are no more stories to load.";
break;
}
?>
剛剛例如我的header.php文件是這樣的:
<?php
// including the config file
require('config.php');
//pdo connct for config.php file
$pdo = connect();
//Include main variables function
include('php/mainvariables.php');
//return of main variables function
$specificSchool = mainvariables();
$shcoolOfficialId = $specificSchool[0]["id"];
?>
的header.php中正常工作,但load_more.php沒有。我得到錯誤:在mainvariables.php的第15行調用null上的成員函數fetchAll()。這是返回查詢的行。還有它說Undefined變量:也查詢同一行。
感謝您
那麼,你是否檢查過'load_more.php'中有'$ _GET ['id']'變量? –
此外,問題是不mainvariables.php,因爲它返回它應該返回的,但相反,load_more.php導致所有這個錯誤,因爲load_more.php是一個獨立的PHP文件,所以我認爲它不能得到$ _GET ['' ID']像header.php這將導致mainvariables.php – Gianni
錯誤@u_mulder是實際上我需要幫助你說什麼。我怎麼能從mainvariables.php得到這樣的說法。我認爲我搞砸了,因爲mainvariables.php和load_more.php都在它們自己的文件中,所以它在header.php中的工作原因是它是真正的dom而不僅僅是一個php文件。 – Gianni