任何人都可以帮助我如何在 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;
}
可能吗?谁能帮我解决这个问题