How to escape strings in SQL Server using PHP?

前端 未结 14 1507
我寻月下人不归
我寻月下人不归 2020-11-22 09:36

I\'m looking for the alternative of mysql_real_escape_string() for SQL Server. Is addslashes() my best option or there is another alternative funct

14条回答
  •  逝去的感伤
    2020-11-22 09:43

    Why would you bother escaping anything when you can use parameters in your query?!

    sqlsrv_query(
        $connection, 
        'UPDATE some_table SET some_field = ? WHERE other_field = ?', 
        array($_REQUEST['some_field'], $_REQUEST['id'])
    )
    

    It works right in selects, deletes, updates regardless whether your values parameters are null or not. Make a matter of principle - Don't concatenate SQL and you are always safe and your queries read much better.

    http://php.net/manual/en/function.sqlsrv-query.php

提交回复
热议问题