How can I prevent SQL injection with dynamic tablenames?

后端 未结 3 1042
青春惊慌失措
青春惊慌失措 2020-11-22 13:08

I had this discussion with a high reputation PHP guy:

PDO has no use here. as well as mysql_real_escape_string. extremely poor quality.

3条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 13:52

    In order to answer how to actually fix the code:

    '...FROM `' . str_replace('`', '``', $tableName) . '`...'
    

    This duplicates all backticks in the table name (this is how escaping in MySQL is done).

    One thing I'm not sure about, is whether this is "encoding-safe" (how does one call it correctly?). One typically recommends mysql_real_escape_string instead of addslashes, because the former takes the encoding of the MySQL connection into account. Maybe this problem applies here, too.

提交回复
热议问题