MySQLi: query VS prepare

前端 未结 2 406
你的背包
你的背包 2020-12-10 05:36

There is something I don\'t quite understand it at all which is prepare and query in mysqli.

This one is using mysqli::query t

2条回答
  •  温柔的废话
    2020-12-10 05:57

    Prepared statements are preferable to plain SQL queries when you are using parameters to dynamically generate the query. In you example, your SQL contains no variables, so using a plain query or prepared statement are functionally equivalent.

    When you must change the values of parameters, in the WHERE clause, for example, then prepared statements will give you added security:

    ...
    WHERE col1 = ? AND col2 = ?
    

    But when your query is simple and fixed, it may require less code to use $mysqli->query($sql) along with fetch_assoc(). Using direct queries rather than prepared statements is not a universally bad practice, as some might have you believe. When your query requires parameterization, or when the same query must be compiled and executed repeatedly, then you'll benefit from the prepared statement.

提交回复
热议问题