0

I'm writing an OOP database class for mysqli. I want all queries to be prepared, but I'm having difficulties binding parameters to queries dynamically. I want the query method API to look like this

query($sql, $param)

However, I don't know how to bind the parameters dynamically within the method. I searched for examples in PHP manual and saw codes like these:

$method = new ReflectionMethod('mysqli_stmt', 'bind_param'); 
$method->invokeArgs($stmt, $params);    
$stmt->execute(); 

I know the ReflectionMethod() will execute the method for the object or class, but I don't understand how it works in this case, considering that each parameter has a type which should be specified while binding.

How can I bind the parameters dynamically?

Secondly, how does ReflectionMethod() work in this situation?

Chibuzo
  • 6,112
  • 3
  • 29
  • 51

1 Answers1

0

I have always use this custom function I wrote to work on mysqli and it works for me.

public function execute_prepared_query_reflection ($array_list)
    {
        $ret_stmt = mysqli_prepare($this->conn, $this->sql);    
        //call_user_func_array('mysqli_stmt_bind_param', array_merge (array($this->statement, $param_listtypes), $param_list));
        //$this->statement->bind_param("issdssssss", $param_staffid, $param_duration, $param_dateappraised, $param_totalmark, $param_detailBySuperv, $param_sumByHead, $param_recommendation, $param_enteredby);
        if ($ret_stmt!=false)
        {
            $method    = new ReflectionMethod('mysqli_stmt', 'bind_param');
            $method->invokeArgs($ret_stmt, $this->createArrayRef($array_list));               
            $retvar = $ret_stmt->execute();  
        } 
        $method = null;
        $ret_stmt = null;

    }   
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345