bind_param () dynamic

1

I have the following class:

class BindParam{
    private $values = array(), $types = '';

    public function add( $type, &$value ){
        $this->values[] = $value;
        $this->types .= $type;
    }

    public function get(){
        return array_merge(array($this->types), $this->values);
    }
} 

and this is my code for generating sql:

if(!empty($dataIni)){
    $qArray[] = "BETWEEN V.DATA_EMISSAO ?";
    $bindParam->add('s', $dataIni);
}
if(!empty($dataFim)){
    $qArray[] = "?";
    $bindParam->add('s', $dataFim);
}
$queryNFe .= implode(' AND ', $qArray);
$param = $bindParam->get();
$sqlNFe = $mysqli->prepare($queryNFe);
call_user_func_array(array($sqlNFe, 'bind_param'), $param);
$sqlNFe->execute();

The query clause is mounting right, the error is in call_user_func_array ()

  Warning: call_user_func_array () expects parameter 1 to be a valid callback, first array member is not a valid class name or object in

I know it's in the first parameter but I can not see what's wrong

I've changed the code:

if(!empty($dataIni)){
    $qArray[] = "V.DATA_EMISSAO BETWEEN ?";
    $bindParam->add('s', $dataIni);
}
if(!empty($dataFim)){
    $qArray[] = "?";
    $bindParam->add('s', $dataFim);
}
$queryNFe .= implode(' AND ', $qArray);
$param = $bindParam->get();
$sqlNFe = $mysqli->prepare($queryNFe) or die($mysqli->error);
call_user_func_array(array($sqlNFe, 'bind_param'), $bindParam->get());
//$sqlNFe->bind_param('ss', $dataIni, $dataFim); 
$sqlNFe->execute();

But now the error is this:

  

Warning: Parameter 2 to mysqli_stmt :: bind_param () expected to be a reference, value given in

    
asked by anonymous 09.11.2015 / 18:14

1 answer

1

I resolved here with this function:

function refValues($arr){
    if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
    {
        $refs = array();
        foreach($arr as $key => $value)
            $refs[$key] = &$arr[$key];
        return $refs;
    }
    return $arr;
}

I just changed it:

call_user_func_array(array($sqlNFe, 'bind_param'), refValues($param));

Thank you

    
09.11.2015 / 19:45