准备好选择查询功能

时间:2014-01-20 14:26:45

标签: php mysqli

我正在尝试为我的DB-class编写一个准备好的select查询函数。我想从下面的代码中你可以理解我想要做什么,但显然这段代码不起作用。怎么做对了?

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!");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

答案:

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));