PHP上的动态bind_result

Bry*_*yan 0 php

可能重复:
动态绑定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;

    }
Run Code Online (Sandbox Code Playgroud)

可能吗?任何人都可以帮助我如何解决这个问题

new*_*rey 10

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

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

SUM(开幕词中缺少一个右括号.最重要的是,你有一个浮动".$year.",它不是任何封闭方法的一部分(虽然,它可以用作别名;但我不习惯看到它而不是先行AS- 所以这可能是我的错误).为了猜测,我会说".$year."要用a 替换它,)那应该修复那个部分:

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

如果$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++;
    }
Run Code Online (Sandbox Code Playgroud)