How are strings escaped for each database extension in php?

三世轮回 提交于 2019-12-05 15:48:57
ThiefMaster

PostgreSQL and others (PDO)

PostgreSQL can use pg_escape_string for string escaping.

For PostgreSQL you don't need any escaping thanks to pg_query_params()

Besides that, you should use PDO with prepared statements. They take care of it and you can pass arguments separately; just like with pg_query_params()

ThiefMaster

MongoDB

In MongoDB you do not write SQL but work with objects ("documents") - you don't have to escape things as you never use strings except for data.

However, you do need to ensure that you actually pass strings and not arrays to the MongoDB API. At least in PHP passing an array such as array('$ne' => 1) would result in an != 1 check and thus be similarly dangerous as SQL injection. And unfortunately PHP allows the client to create arrays inside $_POST etc. by simply providing a field with a name using the PHP array syntax such as password[$ne]. [Source]

OCI8

As far as I can tell:

function oci_escape_string( $str )
{
  return strtr( $str, array( "'" => "''" ) );
}

Should do the trick, ignoring wildcard chars for LIKE statements.

CUBRID

According to its manual, CUBRID PHP API has the same syntax as MySQL, i.e. cubrid_real_escape_string. With its new CUBRID 8.4.0 release it provides 90% compatibility with MySQL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!