MySQLi:查询VS准备

lau*_*kok 10 php data-binding mysqli fetch

有一些我完全不了解它的东西preparequerymysqli.

这个mysqli::query用于处理查询,并且已知缺乏安全性:

public function fetch_assoc($query)
    {
        $result = parent::query($query);
        //$result = self::preparedStatement($query);
        if($result) 
        {
            return $result->fetch_assoc();
        } 
        else
        {
            # call the get_error function
            return self::get_error();
            # or:
            # return $this->get_error();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是一个prepare-bind-execute,它具有更好的安全性,我假设,

public function fetch_assoc_stmt($sql,$types = null,$params = null)
    {
        # create a prepared statement
        $stmt = parent::prepare($sql);

        # bind parameters for markers
        # but this is not dynamic enough...
        //$stmt->bind_param("s", $parameter);

        if($types&&$params)
        {
            $bind_names[] = $types;
            for ($i=0; $i<count($params);$i++) 
            {
                $bind_name = 'bind' . $i;
                $$bind_name = $params[$i];
                $bind_names[] = &$$bind_name;
            }
            $return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
        }

        # execute query 
        $stmt->execute();

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

        # close statement
        $stmt->close();
    }
Run Code Online (Sandbox Code Playgroud)

但是,这两种方法都返回相同的结果,

$mysqli = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);

$sql = "
SELECT *
FROM root_contacts_cfm
ORDER BY cnt_id DESC
";
print_r($mysqli->fetch_assoc_stmt($sql));

print_r($mysqli->fetch_assoc($sql));
Run Code Online (Sandbox Code Playgroud)

他们打印这个:

Array
(
    [cnt_id] => 2
    [cnt_email1] => lau@xx.net
    [cnt_email2] => 
    [cnt_fullname] => Lau T
    [cnt_firstname] => Thiam
    [cnt_lastname] => Lau
    [cnt_organisation] => 
    [cnt_website] => 
    [cnt_biography] => 
    [cnt_gender] => 
    [cnt_birthday] => 
    [cnt_address] => 
    [cnt_postcode] => 
    [cnt_telephone] => 
    [cnt_note] => 
    [cnt_key] => 
    [cat_id] => 
    [tcc_id] => 
    [cnt_suspended] => 0
    [cnt_created] => 2011-02-04 00:00:00
    [cnt_updated] => 2011-02-04 13:54:36
)
Array
(
    [cnt_id] => 2
    [cnt_email1] => lau@xx.net
    [cnt_email2] => 
    [cnt_fullname] => Lau T
    [cnt_firstname] => Thiam
    [cnt_lastname] => Lau
    [cnt_organisation] => 
    [cnt_website] => 
    [cnt_biography] => 
    [cnt_gender] => 
    [cnt_birthday] => 
    [cnt_address] => 
    [cnt_postcode] => 
    [cnt_telephone] => 
    [cnt_note] => 
    [cnt_key] => 
    [cat_id] => 
    [tcc_id] => 
    [cnt_suspended] => 0
    [cnt_created] => 2011-02-04 00:00:00
    [cnt_updated] => 2011-02-04 13:54:36
)
Run Code Online (Sandbox Code Playgroud)

你应该注意到fetch_assoc_stmt我根本不使用的方法fetch_assoc.可能根本没有机会使用它作为prepare使用不同的方式返回结果.

所以,我的问题是,因为使用prepare比使用更好query,为什么要fetch_assoc存在呢?我们不应该忘记它或不应该php.net已弃用?它是一样的fetch_all- 为什么我们应该首先拥有它!??

谢谢.

Mic*_*ski 22

当您使用参数动态生成查询时,预准备语句优于纯SQL查询.在您的示例中,您的SQL不包含任何变量,因此使用普通查询或预准备语句在功能上是等效的.

当您必须更改参数的值时,WHERE例如,在子句中,准备好的语句将为您提供额外的安全性:

...
WHERE col1 = ? AND col2 = ?
Run Code Online (Sandbox Code Playgroud)

但是,当你的查询简单,固定的,它可能需要使用更少的代码$mysqli->query($sql)沿fetch_assoc().使用直接查询而不是准备好的语句并不是一种普遍不好的做法,因为有些人可能会相信.当您的查询需要参数化,或者必须重复编译和执行相同的查询时,您将从准备好的语句中受益.