繁体   English   中英

如何包含一个php文件并在另一个php文件中获取函数值?

[英]How can i include a php file and get the function values in another php file?

所以我有2个文件,一个是mainvariables.php和load_more.php。 因为我对它进行了ajax调用,所以load_more.php本身就是一个php文件。 mainvariables.php文件具有1个功能。

这很奇怪,因为在我的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()。 这是返回查询的行。 它还说未定义的变量:也查询同一行。

谢谢

问题是这样的声明:

return $query->fetchAll(PDO::FETCH_ASSOC);

如果前面的if语句评估为false,则将永远不会定义$query 如果$schoolId不存在,则返回一个不同的值,并在脚本的其余部分中使用它之前检查结果。

您在if statement的范围内定义$query ,因此尚未在if statement之外创建该变量,因此在运行此if statement时,

return  $query->fetchAll(PDO::FETCH_ASSOC);

它会失败。 你为什么不试试这个

function mainVariables(){
    global $pdo;

    $query = null;

    //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);
}

编辑1

我将获取结果放入if statement ,因此只有它会在该部分中获取,这样您就会收到错误,而无需在if statement之外定义$query

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));

        //all values of current school
        return  $query->fetchAll(PDO::FETCH_ASSOC);
    }else {
        echo "Not Working";
    }
}

暂无
暂无

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

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