简体   繁体   English

mysqli中的bind_param不适用于PHP

[英]bind_param in mysqli is not working with PHP

I was trying to learn MySqli prepared statements, but i got stuck in dynamic binding. 我试图学习MySqli准备的语句,但是我陷入了动态绑定中。

This is my code. 这是我的代码。 this is working fine but i am getting all the results instead of user_id=1 . 这工作正常,但我得到所有结果而不是user_id = 1 not sure what i am missing here. 不知道我在这里想念的是什么。 please help me.. 请帮我..

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

i am calling this function as below. 我正在按以下方式调用此函数。

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

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

In $bind condition you concat your SQL but you prepare it before condition. $bind条件中,您可以连接SQL但要在条件之前进行准备。 I suggest you to prepare query after completing full query string. 我建议您在完成完整的查询字符串后准备查询。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM