du59131 2015-09-08 18:42
浏览 96

功能不返回值

I just asked a question earlier, but now i have formatted it into a function that does not return any value.

This is my code:

echo GetHours($UID, $DAY, $MONTH, $YEAR);

function GetHours($UserId, $Day, $Month, $Year){
//filter queries:


    //YEAR:
    if($Year==FALSE){
        $Y = "";
    } else {
        $Y = " AND Year = '$Year'";
    }

    //MONTH:
    if($Month==FALSE){
        $M = "";
    } else {
        $M = " AND Month = '$Month'";
    }

    //DAY:
    if($Day==FALSE){
        $D = "";
    } else {
        $D = " AND Day = '$Day'";
    }

    $Query = mysql_query("SELECT SUM(TotalHrs) 
                          FROM WorkLog 
                          WHERE UserId = '$UserId'$D$M$Y");

    $Data = mysql_fetch_array($Query);

    return $Data;

}

Now, i do know that mysql_ functions are depreciated, but its required for this application at the moment.

My current problem is that this function does not return anything after using GET parameters to test.

Any solutions to this?

EDIT

I have changed the last lines to: return json_encode($Data); and now the screen shows: {"0":"8","SUM(TotalHrs)":"8"}

  • 写回答

2条回答 默认 最新

  • dsfsdfsd34324 2015-09-08 18:51
    关注

    Always check that mysql_query() has not returned an error! Specially if you are building your query from parts generated from inputs that may or may not be present.

    This should at least identify any errors in the SQL.

    function GetHours($UserId, $Day, $Month, $Year){
    //filter queries:
    
    
        //YEAR:
        if($Year==FALSE){
            $Y = "";
        } else {
            $Y = " AND Year = '$Year'";
        }
    
        //MONTH:
        if($Month==FALSE){
            $M = "";
        } else {
            $M = " AND Month = '$Month'";
        }
    
        //DAY:
        if($Day==FALSE){
            $D = "";
        } else {
            $D = " AND Day = '$Day'";
        }
    
        $Query = mysql_query("SELECT SUM(TotalHrs) 
                              FROM WorkLog 
                              WHERE UserId = '$UserId'$D$M$Y");
    
        if ( ! $Query ) {
            // just debug code, should be amended for a live site situation
            echo mysql_error();
            return 'Its broken';
        }
    
        $Data = mysql_fetch_array($Query);
    
        return $Data;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计