如何在 SQL 查询中使用准备好的和绑定的语句

发布于 2024-10-18 14:25:39 字数 1984 浏览 1 评论 0原文

有人告诉我,下面的查询非常容易受到 sql 注入的影响 - 我应该使用绑定参数,

class search
{

    public $mysqli = null;

    public function __construct($mysqli,$keyword = null)
    {
       $this->mysqli = $mysqli;
    }

    public function get_result($parameter)
    {
        $sql = "
        SELECT *
        FROM root_contacts_cfm
        WHERE root_contacts_cfm.cnt_id = '".$parameter."'
        ORDER BY cnt_id DESC
        ";

        $item = $this->mysqli->fetch_assoc($sql);
        return $item;
    }
}

我可以问一下 - 如何使用准备好的绑定语句来转换这个 search 类?

我在网上读过一些文章为什么我们应该使用准备好的语句, 第 1 条 第 2 条

但我仍然不知道不知道如何改进我的查询...我尝试了下面的修改,

class search
{

    public $mysqli = null;

    public function __construct($mysqli)
    {
       $this->mysqli = $mysqli;
    }

    public function get_result($parameter)
    {
        $sql = "
        SELECT *
        FROM root_contacts_cfm
        WHERE root_contacts_cfm.cnt_id = '?'
        ORDER BY cnt_id DESC
        ";

        $stmt = $this->mysqli->prepare($sql);

        /* bind parameters for markers */
        $stmt->bind_param("s", $parameter);

        /* execute query */
        $stmt->execute();

         /* fetch value */
        return $stmt->fetch();

    }
}

所以当我将搜索类作为对象调用时,

$mysqli = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$output = new search($mysqli);
print_r($output->get_result('1'));

我会收到此错误,

警告:mysqli_stmt::bind_param() [mysqli-stmt.bind-param]:数量 变量数量不匹配 准备好的语句中的参数 C:\wamp\www\xxxl\class_database.php 上 第487行

第 487 行指的是 $stmt->bind_param("s", $parameter);

谢谢。

I have been told that my query below is very susceptible to an sql injection - I should be using bound parameters instead,

class search
{

    public $mysqli = null;

    public function __construct($mysqli,$keyword = null)
    {
       $this->mysqli = $mysqli;
    }

    public function get_result($parameter)
    {
        $sql = "
        SELECT *
        FROM root_contacts_cfm
        WHERE root_contacts_cfm.cnt_id = '".$parameter."'
        ORDER BY cnt_id DESC
        ";

        $item = $this->mysqli->fetch_assoc($sql);
        return $item;
    }
}

can I ask - how can I turn this search class with a prepared and bound statement?

I have read some articles online why we should use prepared statements,
article 1
article 2

But I still don't have a clue how to improve my query... I tried with this amendment below,

class search
{

    public $mysqli = null;

    public function __construct($mysqli)
    {
       $this->mysqli = $mysqli;
    }

    public function get_result($parameter)
    {
        $sql = "
        SELECT *
        FROM root_contacts_cfm
        WHERE root_contacts_cfm.cnt_id = '?'
        ORDER BY cnt_id DESC
        ";

        $stmt = $this->mysqli->prepare($sql);

        /* bind parameters for markers */
        $stmt->bind_param("s", $parameter);

        /* execute query */
        $stmt->execute();

         /* fetch value */
        return $stmt->fetch();

    }
}

So when I call the search class as an object,

$mysqli = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$output = new search($mysqli);
print_r($output->get_result('1'));

I will get this error,

Warning: mysqli_stmt::bind_param()
[mysqli-stmt.bind-param]: Number of
variables doesn't match number of
parameters in prepared statement in
C:\wamp\www\xxxl\class_database.php on
line 487

line 487 refers to $stmt->bind_param("s", $parameter);

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情哥ㄟ 2024-10-25 14:25:39

尝试删除 ',它围绕着 ? 占位符。

$sql = "
SELECT *
FROM root_contacts_cfm
WHERE root_contacts_cfm.cnt_id = ?
ORDER BY cnt_id DESC
";

在准备好的语句中,整个事情就是在绑定参数时指定参数的类型,而不是在 SQL 查询中 - 你在编写 '?' 时就是这样做的。您说过,当您将 param 绑定为字符串时,它必须是字符串,但不是必需的。数据库引擎现在将如何插入/转义该值。

Try removing ', which surrounds your ? placeholder.

$sql = "
SELECT *
FROM root_contacts_cfm
WHERE root_contacts_cfm.cnt_id = ?
ORDER BY cnt_id DESC
";

In prepared statements whole thing is about specifying type of param when you bind it, not in SQL query - which you did, when wrote '?' . You've said that has to be string, but is not required, when you bind param as a string. Database engine will now how to insert/escape that value.

小鸟爱天空丶 2024-10-25 14:25:39

这是我在网上搜索后的解决方案:

class search
{
    public $mysqli = null;

    public function __construct($mysqli)
    {
       $this->mysqli = $mysqli;
    }

    public function get_result($parameter)
    {
        $sql = "
        SELECT *
        FROM root_contacts_cfm
        WHERE root_contacts_cfm.cnt_id = ?
        ORDER BY cnt_id DESC
        ";

        # create a prepared statement
        $stmt = $this->mysqli->prepare($sql);

        # bind parameters for markers
        $stmt->bind_param("s", $parameter);

        # execute query 
        $stmt->execute();



        /*
        # these lines of code below return multi-dimentional array, similar to mysqli::fetch_all()
        $stmt->store_result();

        $variables = array();
        $data = array();
        $meta = $stmt->result_metadata();

        while($field = $meta->fetch_field())
            $variables[] = &$data[$field->name]; // pass by reference

        call_user_func_array(array($stmt, 'bind_result'), $variables);

        $i=0;
        while($stmt->fetch())
        {
            $array[$i] = array();
            foreach($data as $k=>$v)
                $array[$i][$k] = $v;
            $i++;
        }

        return $array;
        */

        # these lines of code below return one dimentional array, similar to mysqli::fetch_assoc()
        $meta = $stmt->result_metadata(); 

        while ($field = $meta->fetch_field()) { 
            $var = $field->name; 
            $var = null; 
            $parameters[$field->name] = &$var; 
        }

        call_user_func_array(array($stmt, 'bind_result'), $parameters); 

        while($stmt->fetch()) 
        { 
            return $parameters;
            //print_r($parameters);      
        } 

        # the commented lines below will return values but not arrays
        # bind result variables
        //$stmt->bind_result($id); 

        # fetch value
        //$stmt->fetch(); 

        # return the value
        //return $id; 

        # close statement
        $stmt->close();
    }
}

虽然不太明白...

我发现很难理解和练习准备好的和绑定的语句... mysqli::query() 对我来说更容易理解...

this is my solution after searching around online:

class search
{
    public $mysqli = null;

    public function __construct($mysqli)
    {
       $this->mysqli = $mysqli;
    }

    public function get_result($parameter)
    {
        $sql = "
        SELECT *
        FROM root_contacts_cfm
        WHERE root_contacts_cfm.cnt_id = ?
        ORDER BY cnt_id DESC
        ";

        # create a prepared statement
        $stmt = $this->mysqli->prepare($sql);

        # bind parameters for markers
        $stmt->bind_param("s", $parameter);

        # execute query 
        $stmt->execute();



        /*
        # these lines of code below return multi-dimentional array, similar to mysqli::fetch_all()
        $stmt->store_result();

        $variables = array();
        $data = array();
        $meta = $stmt->result_metadata();

        while($field = $meta->fetch_field())
            $variables[] = &$data[$field->name]; // pass by reference

        call_user_func_array(array($stmt, 'bind_result'), $variables);

        $i=0;
        while($stmt->fetch())
        {
            $array[$i] = array();
            foreach($data as $k=>$v)
                $array[$i][$k] = $v;
            $i++;
        }

        return $array;
        */

        # these lines of code below return one dimentional array, similar to mysqli::fetch_assoc()
        $meta = $stmt->result_metadata(); 

        while ($field = $meta->fetch_field()) { 
            $var = $field->name; 
            $var = null; 
            $parameters[$field->name] = &$var; 
        }

        call_user_func_array(array($stmt, 'bind_result'), $parameters); 

        while($stmt->fetch()) 
        { 
            return $parameters;
            //print_r($parameters);      
        } 

        # the commented lines below will return values but not arrays
        # bind result variables
        //$stmt->bind_result($id); 

        # fetch value
        //$stmt->fetch(); 

        # return the value
        //return $id; 

        # close statement
        $stmt->close();
    }
}

not quite understand it though...

I find it difficult to understand and practice with prepared and bound statement... mysqli::query() is easier for me to understand...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文