我之前刚问过一个问题,但现在我已将其格式化为一个不会返回任何值的函数。
这是我的代码:
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;
}
现在,我知道mysql_
函数已被折旧,但目前此应用程序需要它。
我目前的问题是,在使用GET参数进行测试后,此函数不会返回任何内容。
对此有何解决方案?
修改
我已将最后一行更改为:return json_encode($Data);
,现在屏幕显示:{"0":"8","SUM(TotalHrs)":"8"}
答案 0 :(得分:0)
始终检查mysql_query()
是否未返回错误!特别是,如果您从可能存在或可能不存在的输入生成的部分构建查询。
这至少应该识别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;
}
答案 1 :(得分:0)
我发现通过echo json_encode(GetHours($UID, $DAY, $MONTH, $YEAR));
我完成的代码有效:
<?php
include 'assets/db_connect.php';
//Function to get total from WorkLog:
$DAY = $_GET['day'];
$MONTH = $_GET['month'];
$YEAR = $_GET['year'];
$UID = $_GET['id'];
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['SUM(TotalHrs)'];
}