How to escape strings in SQL Server using PHP?

前端 未结 14 1509
我寻月下人不归
我寻月下人不归 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:54

    I have been using this as an alternative of mysql_real_escape_string():

    function htmlsan($htmlsanitize){
        return $htmlsanitize = htmlspecialchars($htmlsanitize, ENT_QUOTES, 'UTF-8');
    }
    $data = "Whatever the value's is";
    $data = stripslashes(htmlsan($data));
    
    0 讨论(0)
  • 2020-11-22 09:55

    In order to escape single- and double-quotes, you have to double them up:

    $value = 'This is a quote, "I said, 'Hi'"';
    
    $value = str_replace( "'", "''", $value ); 
    

    $value = str_replace( '"', '""', $value );

    $query = "INSERT INTO TableName ( TextFieldName ) VALUES ( '$value' ) ";
    

    etc...

    and attribution: Escape Character In Microsoft SQL Server 2000

    0 讨论(0)
  • 2020-11-22 09:56

    If you are using PDO, you can use the PDO::quote method.

    0 讨论(0)
  • 2020-11-22 09:57

    It is better to also escape SQL reserved words. For example:

    function ms_escape_string($data) {
        if (!isset($data) or empty($data))
            return '';
    
        if (is_numeric($data))
            return $data;
    
        $non_displayables = array(
            '/%0[0-8bcef]/',        // URL encoded 00-08, 11, 12, 14, 15
            '/%1[0-9a-f]/',         // url encoded 16-31
            '/[\x00-\x08]/',        // 00-08
            '/\x0b/',               // 11
            '/\x0c/',               // 12
            '/[\x0e-\x1f]/',        // 14-31
            '/\27/'
        );
        foreach ($non_displayables as $regex)
            $data = preg_replace( $regex, '', $data);
        $reemplazar = array('"', "'", '=');
        $data = str_replace($reemplazar, "*", $data);
        return $data;
    }
    
    0 讨论(0)
  • 2020-11-22 10:01

    You could roll your own version of mysql_real_escape_string, (and improve upon it) with the following regular expression: [\000\010\011\012\015\032\042\047\134\140]. That takes care of the following characters: null, backspace, horizontal tab, new line, carriage return, substitute, double quote, single quote, backslash, grave accent. Backspace and horizontal tab are not supported by mysql_real_escape_string.

    0 讨论(0)
  • 2020-11-22 10:04

    After struggling with this for hours, I've come up with a solution that feels almost the best.

    Chaos' answer of converting values to hexstring doesn't work with every datatype, specifically with datetime columns.

    I use PHP's PDO::quote(), but as it comes with PHP, PDO::quote() is not supported for MS SQL Server and returns FALSE. The solution for it to work was to download some Microsoft bundles:

    • Microsoft Drivers 3.0 for PHP for SQL Server (SQLSRV30.EXE): Download and follow the instructions to install.
    • Microsoft® SQL Server® 2012 Native Client: Search through the extensive page for the Native Client. Even though it's 2012, I'm using it to connect to SQL Server 2008 (installing the 2008 Native Client didn't worked). Download and install.

    After that you can connect in PHP with PDO using a DSN like the following example:

    sqlsrv:Server=192.168.0.25; Database=My_Database;
    

    Using the UID and PWD parameters in the DSN didn't worked, so username and password are passed as the second and third parameters on the PDO constructor when creating the connection. Now you can use PHP's PDO::quote(). Enjoy.

    0 讨论(0)
提交回复
热议问题