0

可能重复:
动态绑定mysqli_stmt参数,然后绑定结果(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;

    }

可能吗?谁能帮我解决这个问题

4

2 回答 2

10

首先,生成年份字段的代码有一个很好的语法错误:

"SUM(IF(c.datecreated='".$year."',IF(LOWER(c.fullTimeEployeeType)='basic hour rate', c.fullTimeEployeeTypeAmount*2080 , c.fullTimeEployeeTypeAmount),0) ".$year.","

SUM(开场白缺少右括号。最重要的是,您有一个".$year."不属于任何封闭方法的浮动(尽管它可以用作别名;我不习惯在没有前面的情况下看到它AS- 所以这可能是我的错误)。猜测一下,我会说用 a 替换".$year."a)并且应该修复该部分:

$concatYear .= "SUM(IF(c.datecreated='".$year."',IF(LOWER(c.fullTimeEployeeType)='basic hour rate', c.fullTimeEployeeTypeAmount*2080 , c.fullTimeEployeeTypeAmount),0)),";

如果$year添加实际上是一个别名,您可以在它之前添加右括号以关闭该SUM()函数。

关于动态绑定变量,我理想的解决方案实际上来自关于 SO的类似问题/答案(这是直接复制/粘贴,我不相信它,但我确实喜欢它=P):

    // Get metadata for field names
    $meta = $stmt->result_metadata();

    // This is the tricky bit dynamically creating an array of variables to use
    // to bind the results
    while ($field = $meta->fetch_field()) { 
        $var = $field->name; 
        $$var = null; 
        $fields[$var] = &$$var;
    }

    // Bind Results
    call_user_func_array(array($stmt,'bind_result'),$fields);

    // Fetch Results
    $i = 0;
    while ($stmt->fetch()) {
        $results[$i] = array();
        foreach($fields as $k => $v)
            $results[$i][$k] = $v;
        $i++;
    }
于 2012-09-28T02:32:04.653 回答
1

你必须call_user_func_array

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};

}
于 2012-09-28T02:34:02.417 回答