0

I'm trying to write a prepared select query function for my DB-class. I think from the code below you can understand what I want to do but obviously this code doesn't work. How can do it right?

public function preparedSelectQuery($sql, $types){
    if(func_num_args() < 3){
        throw new Exception("Less than 3 args!");
    }
    $stmt = $this->mysqli->stmt_init();
    if (!$stmt->prepare($sql)) {
        throw new Exception("Failed to prepare statement!");
    } else {
        if($stmt->bind_param($types, func_get_args())){
            if($stmt->execute()){
                $result = $stmt->get_result();
                while ($row = $result->fetch_assoc()) {
                    $dataset[] = $row;
                }
                $result->close();
                $stmt->close();
                return $dataset;
            }else{
                throw new Exception("Failed to execute!");
            }
        }else{
            throw new Exception("Failed to bind parameters!");
        }
    }
}
alan_derua
  • 2,252
  • 3
  • 20
  • 19

1 Answers1

0

The answer:

function makeValuesReferenced($arr){
        $refs = array();
        foreach($arr as $key => $value)
            $refs[$key] = &$arr[$key];
        return $refs;

    }
$funcArgs = array_slice(func_get_args(),1);
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($funcArgs));
alan_derua
  • 2,252
  • 3
  • 20
  • 19