“set names” vs mysqli_set_charset — besides affecting mysqli_escape_string, are they identical?

前端 未结 4 586
遥遥无期
遥遥无期 2021-01-04 09:53

It seems to be common knowledge to use mysql_set_charset / mysqli::set_charset instead of the direct MySQL query set names.

The reason often cited is that se

4条回答
  •  庸人自扰
    2021-01-04 10:34

    mysql: the whole interface is deprecated, so don't use any of it at all (PHP 7 removes the interface).

    mysqli (and PDO) has prepared statements that make the use of real_escape_string not needed (nor wanted). -> So if you use mysqli and prepared statements only: no worries how you set the charset.

    Since you care about security: I see little point in not using prepared statements.

    Once you use mysqli's prepared statements the only way forward is to use $mysqli->set_charset() as you can't simply concatenate multiple sql statements in one string anymore.

    Hence the question to know the difference is at most academic and not relevant in real life.

    In summary:

    • mysql: don't use at all.

    • mysqli: use prepared statements and hence the set_charset() method
      Also: you won't need real_escape_string anymore once you use prepared statements.

    • or -of course- use PDO and it's methods.

提交回复
热议问题