可能重复:
Dynamically bind mysqli_stmt parameters and then bind result (PHP)
任何人都可以帮助我如何在PHP上创建动态bind_result。 我的查询字段不知道自动态创建以来有多少字段(例如,根据日期范围创建年份字段)。下面是我的脚本,并突出显示问题在哪里。
public function getMarketingReports($datefrom,$dateto)
{
$yearfrom = date("Y", strtotime($datefrom));
$yearto = date("Y", strtotime($dateto));
//create year fields
$concatYear = "";
for($year=$yearfrom;$year<=$yearto;$year++){
$concatYear .= "SUM(IF(c.datecreated='".$year."',IF(LOWER(c.fullTimeEployeeType)='basic hour rate', c.fullTimeEployeeTypeAmount*2080 , c.fullTimeEployeeTypeAmount),0)) ".$year.",";
}
$reportdata = array();
$db = Connection::Open();
$stmt = $db->stmt_init();
if($stmt->prepare("SELECT p.Code `PositionCode`,
p.name `PositionName`,
l.value `Location`,
".$concatYear."
SUM(b.field205) `TotalEmployees`
FROM c1 c
INNER JOIN b1 b
ON c.registrationid=b.id
INNER JOIN positions p
ON c.positionid=p.id
INNER JOIN lookupvalues l
ON c.location=l.id
WHERE c.`status`!=2
AND c.datecreated BETWEEN ? AND ?
GROUP BY c.positionid,c.location,YEAR(c.datecreated)")){
$datefrom = $datefrom." 00:00:00";
$dateto = $dateto." 23:59:59";
$stmt->bind_param("ss",$datefrom,$dateto);
$stmt->execute();
$stmt->bind_result
(
$positionCode,
$positionName,
$location,
**//getting bind result data here for year fields**
$totalEmployees
);
while($stmt->fetch())
{
$surveydata = array();
$surveydata['positionCode'] = $positionCode;
$surveydata['positionName'] = $positionName;
$surveydata['location'] = $location;
**//storing of data here for year fields**
$surveydata['totalEmployees'] = $totalEmployees;
array_push($reportdata,$surveydata);
}
}
Connection::Close();
return $reportdata;
}
有可能吗?任何人都可以帮我解决这个问题
答案 0 :(得分:10)
首先,生成年字段的代码有一个很好的语法错误:
"SUM(IF(c.datecreated='".$year."',IF(LOWER(c.fullTimeEployeeType)='basic hour rate', c.fullTimeEployeeTypeAmount*2080 , c.fullTimeEployeeTypeAmount),0) ".$year.","
SUM(
开场语句缺少右括号。最重要的是,你有一个浮动".$year."
,它不是任何封闭方法的一部分(但是,它可以用作别名;我不习惯在没有前面{{1}的情况下看到它虽然 - 所以这可能是我的错误)。为了猜测,我会说用AS
替换".$year."
并且应该修复该部分:
)
如果$concatYear .= "SUM(IF(c.datecreated='".$year."',IF(LOWER(c.fullTimeEployeeType)='basic hour rate', c.fullTimeEployeeTypeAmount*2080 , c.fullTimeEployeeTypeAmount),0)),";
添加实际上是别名,则可以在其前面添加右括号以关闭$year
函数。
关于动态绑定变量,我理想的解决方案实际上来自a similar question/answer SO(这是直接复制/粘贴,我不相信它,但我喜欢它= P):
SUM()
答案 1 :(得分:1)
call_user_func_array(array($stmt, "bind_result"), $argArray);
以这种方式填写$argArray
:
$years = array();
$concatYear = "";
for($year=$yearfrom;$year<=$yearto;$year++){
$concatYear .= "SUM(IF(c.datecreated='".$year."',IF(LOWER(c.fullTimeEployeeType)='basic hour rate', c.fullTimeEployeeTypeAmount*2080 , c.fullTimeEployeeTypeAmount),0) ".$year.",";
// track the years you need
$years[] = $year;
}
...
//you prepare all the vars here
$argArray = array(
$positionCode,
$positionName,
$location
);
//loop the years
foreach($years as $y)
{
$argArray[] = ${$y};
}
$argArray[] = $annualSalary;
$argArray[] = $totalEmployees;
$argArray[] = $yearOfData;
多年以后你用这种方式做事:
...
foreach($years as $y)
{
$surveydata[$y] = ${$y};
}