PHP / MySQL函数导致其他人停止工作

时间:2014-11-18 20:55:52

标签: php mysql pdo

我有3个PHP文件:一个index.php,一个带有MySQL数据库定义的config.php和一个带有三个独立函数的functions.php文件。当在索引文件中只调用其中一个函数时,一切正常。然而,当我只添加其他两个中的一个时,事情就会停止工作。

使用以下示例/文件,我收到此错误消息,其中第二个函数属于index.php代码: 错误:致命错误:在...第37行中的非对象上调用成员函数query(),这对应于第二个函数中的数据库查询 - get_chapter_dollars函数。

我确定它很简单,但我错过了什么,在这里?为什么第二个功能会导致问题?

CONFIG.PHP

<?php
define("BASE_URL","/dayofgiving/");
define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"] . "/dayofgiving/");

// Set variables for the database.

define("DB_HOST", "localhost");
define("DB_NAME", "cdwyer_dspdog");
define("DB_PORT", "1234");
define("DB_USER", "user");
define("DB_PASS", "pass");

的functions.php

<?php

require_once($_SERVER["DOCUMENT_ROOT"] . "/dayofgiving/inc/config.php");

function get_state_stats() {
    require_once(ROOT_PATH . "inc/db.php");
    try {
        $results = $db->query("
        SELECT abb, fullname, dollars, alumnidollars, ugdollars, donors, alumnidonors, ugdonors
        FROM dog_states");
        $maxDollars = $db->query("
        SELECT MAX(dollars) 
        FROM dog_states;
        ");
    } catch (Exception $e) {
        echo "Data coudn’t be found.";
        exit;
    }

    $maxDollars = intval($maxDollars->fetchColumn(0));

    $stateInfo=array();

    while ($row = $results->fetch(PDO::FETCH_ASSOC)) { // Will return boolean(false) when condition is no longer met.
        $stateAbb = $row["abb"];
        $thisDollars = $row["dollars"];
        $stateInfo[$stateAbb] = $row; // Loops through all states/rows one at a time, and adds all properties to the stateInfo variable w/ abbreviation as key.
        $stateInfo[$stateAbb]["opacity"] = ((($thisDollars / $maxDollars)*.5)+.5);
    }

    return $stateInfo;
}

function get_chapter_dollars() {
    require_once(ROOT_PATH . "inc/db.php");
    try {
        $dollarResults = $db->query("
        SELECT abb, fullname, dollars
        FROM dog_chapters
        ORDER BY dollars DESC
        LIMIT 5");
    } catch (Exception $e) {
        echo "Data coudn’t be found.";
        exit;
    }

    $chapterDollars=array();

    while ($row = $dollarResults->fetch(PDO::FETCH_ASSOC)) { // Will return boolean(false) when condition is no longer met.
        $chapterDollars[] = $row; // Loops through all (ie, top 5) chapters/rows one at a time, and adds all properties to the chapterDollars variable w/ number as key.
    }

    return $chapterDollars;
}

function get_chapter_donors() {
    require_once(ROOT_PATH . "inc/db.php");
    try {
        $donorResults = $db->query("
        SELECT abb, fullname, donors
        FROM dog_chapters
        ORDER BY donors DESC
        LIMIT 5");
    } catch (Exception $e) {
        echo "Data coudn’t be found.";
        exit;
    }

    $chapterDonors=array();

    while ($row = $donorResults->fetch(PDO::FETCH_ASSOC)) { // Will return boolean(false) when condition is no longer met.
        $chapterDonors[] = $row; // Loops through all (ie, top 5) chapters/rows one at a time, and adds all properties to the chapterDollars variable w/ number as key.
    }

    return $chapterDonors;
}

?>

INDEX.PHP(部分)

<?php require_once($_SERVER["DOCUMENT_ROOT"] . "/dayofgiving/inc/config.php"); ?>
    <?php require_once(ROOT_PATH . "db/functions.php"); ?>
<?php $tempStateStats = get_state_stats() ?>
var jsonStates = <?php echo json_encode($tempStateStats); ?>;
<?php $returnChapterDollars = get_chapter_dollars() ?>
<?php echo $returnChapterDollars[0]["abb"];?>

1 个答案:

答案 0 :(得分:1)

我遇到过require_once与我的PHP版本(5.3.3)正常工作的问题。出于测试目的,尝试将其更改为要求或包含页面上的每个实例,并查看它是否有效。