Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8589479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:04:01+00:00 2026-06-11T23:04:01+00:00

Possible Duplicate: Dynamically bind mysqli_stmt parameters and then bind result (PHP) can any one

  • 0

Possible Duplicate:
Dynamically bind mysqli_stmt parameters and then bind result (PHP)

can any one help me on how could I create a dynamic bind_result on PHP.
My query field doesn’t know how many fields are there since it was created dynamically (e.g creating year fields base on the date range). below is my script and highlighted on where is the problem.

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;

    }

Is it possible? Can anyone help me on how could I solve this problem

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:04:02+00:00Added an answer on June 11, 2026 at 11:04 pm

    To start, your code that generates the year fields has a nice syntax error:

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

    The SUM( opening statement is missing a closing parentheses. On top of that, you have a floating ".$year." that isn’t part of any of the enclosed methods (though, it could be used as an alias; I’m not used to seeing it without a preceeding AS though – so this could be my mistake). To take a guess, I would say to replace the ".$year." with a ) and that should fix that part:

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

    If the $year addition is in fact to be an alias, you could add the closing parentheses immediately before it to close the SUM() function.

    Regarding dynamically binding the variables, my ideal solution actually comes from a similar question/answer on SO (this is a direct copy/paste and I take no credit for it, but I do like it =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++;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Issue regarding dynamically loading Images in loop using Java Swing Can any
Possible Duplicate: how to add button dynamically in android? How can I dynamically create
Possible Duplicate: How can I access local scope dynamically in javascript? Hi all. We
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Dynamically retrieving current method's name Obj-C introspection: How can a method reference
Possible Duplicate: How can I merge properties of two JavaScript objects dynamically? I have
Possible Duplicate: Dynamically adding collapsible elements I would like to know how I could
Possible Duplicate: Dynamically Growing an Array in C++ I want to create a loop
Possible Duplicate: Dynamically creating/inserting into an associative array in PHP I have the following
Possible Duplicate: How to create a multiline UITextfield? How can I implement a multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.