mysqli中的bind_param不能与PHP一起使用

时间:2015-07-26 05:50:07

标签: php mysqli

我试图学习MySqli预备语句,但我陷入了动态绑定。

这是我的代码。这工作正常,但我得到的所有结果,而不是 user_id = 1 。不知道我在这里失踪了什么。请帮帮我..

public function prepareSelectSql($from,$feilds="*",$where = '',$bind=false,$params)
{
    if($this->conn)
    {
        $query = "SELECT ".$feilds." FROM `".$from."`";

        $stmt = $this->conn->prepare($query);

        if($stmt === false) 
        {
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $this->conn->errno . ' ' . $this->conn->error, E_USER_ERROR);
        } 

        if($bind)
        {
            echo "<br/><br/>";
            echo $query .= $where;
            $id='1';//call_user_func_array(array($stmt, 'bind_param'), $params);
            $stmt->bind_param("i",$id);
            echo "<br/><br/>Here";
        }   

        $stmt->execute();

        $result = $stmt->get_result();

        while ($myrow = $result->fetch_assoc())
        {
            print_r($myrow);
        }

    }
    else { echo "Not Aavailable";}
}

我正在调用此函数,如下所示。

$where = 'WHERE `ID`=?';
        $params = array('i','1');
        $feilds = '`user_nicename`';

        $this->db->prepareSelectSql('wp_users',$feilds,$where,true,$params);

1 个答案:

答案 0 :(得分:2)

$bind条件下,您将SQL连成,但是在条件之前准备它。我建议你在完成完整的查询字符串后准备查询。

$query = "SELECT ".$feilds." FROM `".$from."` ";

if($stmt === false){
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $this->conn->errno . ' ' . $this->conn->error, E_USER_ERROR);
}

if($bind){
    $query .= $where;
    $stmt = $this->conn->prepare($query);
    $id = '1';
    $stmt->bind_param("i",$id);
}else{
    $stmt = $this->conn->prepare($query);
}
相关问题