gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:34:30 24 4
gpt4 key购买 nike

我有 3 个 PHP 文件:一个 index.php、一个包含 MySQL 数据库定义的 config.php 和一个包含三个独立函数的functions.php 文件。当索引文件中仅调用其中一个函数时,一切正常。然而,当我只添加另外两个之一时,事情就停止了。

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

我确信这很简单,但是我在这里缺少什么?为什么第二个函数会导致问题?

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

函数.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"];?>

最佳答案

我在使用我的 PHP 版本 (5.3.3) 时遇到了 require_once 无法正常工作的问题。为了测试的目的,尝试将其更改为需要或包含页面上的每个实例,看看它是否有效。

关于PHP/MySQL 函数导致其他函数停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27003852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com