【问题标题】:Trying to call two functions within function PHP试图在函数 PHP 中调用两个函数
【发布时间】:2013-06-11 23:57:57
【问题描述】:

我觉得这是一个愚蠢的问题,但我想不通。

我有一个 php 文件,它需要对数据库执行最少次数的 ping 操作。我有两个函数使用来自 ping 的相同数据,我试图在一个函数中调用这些函数。

每个函数循环遍历 mysql 查询结果并检查不同的条件,如果满足条件,它会调用另一个函数。问题是一旦第一个函数执行完毕,第二个函数就不会执行。

请帮忙。

function gather_info(){
$cash_request = check_user_cash_on_hand();
if($cash_request != false){
    check_for_overpay($cash_request);
    check_for_sabotage($cash_request);
}
}


function check_for_overpay($cash_requests){
if(mysql_num_rows($cash_requests) > 0){
    $days_possible_amount = get_days_ads_amount();
    if($days_possible_amount != false){
        $first_bucket = 1/3 * $days_possible_amount;
        $second_bucket = 2/3 * $days_possible_amount;
        while($row = mysql_fetch_assoc($cash_requests)){
            $cash_id = $row["cash_id"];
            $cash_request = $row["CASH"];


            if($cash_request <= $first_bucket){
                echo "I'm in one";



            }else if($cash_request > $first_bucket && $cash_request <= $second_bucket){
                echo "I'm in two";

            }else if($cash_request > $second_bucket){
                notify_admin_of_suspicious_amount($row, "not_possible");

            }
        }
    }   
}   

}


/* CHECKS IF SUM OF CASH REQUESTS PER USER GREATER THAN 5 and LESS THAN OR EQUAL TO HIS EARNED AMOUNT */
function check_for_sabotage($cash_request){
if(mysql_num_rows($cash_request) > 0){
    while($row = mysql_fetch_array($cash_request)){
        $requested_cash = $row["CASH"];
        $available_cash = $row["cash_amount"];

        if($requested_cash > 5 && $requested_cash <= $available_cash){
            echo "you got me";
        }else{
            notify_admin_of_suspicious_amount($row, "not_enough");
        }
    }
}   
}

function notify_admin_of_suspicious_amount($row, $type){

echo "suspicious";


}

基本上,如果 check_for_overpay 完成,脚本就会停止并且 check_for_sabotage 永远不会被调用。有什么建议吗?

【问题讨论】:

  • 那么,check_for_overpay 是做什么的?
  • 你可以在我的问题中看到我解释了每个函数的作用。
  • 是的,这不是我要问的。您希望我们如何帮助您处理我们看不到的代码?
  • 好的,我对其进行了编辑以反映这一点。
  • BASIC 是一种不同的编程语言;请不要滥用标签!

标签: php function scope


【解决方案1】:

您在函数 check_for_overpay() 内部调用 mysql_num_rows()。它需要的连接mysql_connect() 可能在函数的变量范围之外,因为它没有被传递,并且在该函数中没有调用建立连接的函数。

还要注意mysql_* 函数have been deprecated as of PHP 5.5。您应该使用 MySQLi 或 PDO。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    相关资源
    最近更新 更多