本文介绍了如何使用PHP在SQL Server中转义字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为SQL Server寻找 mysql_real_escape_string()的替代方案。

I'm looking for the alternative of mysql_real_escape_string() for SQL Server. Is addslashes() my best option or there is another alternative function that can be used?

编辑:替代 mysql_error()也将是有用的。

Edit: Alternative for mysql_error() would also be useful.

推荐答案

addslashes()不完全足够,但PHP的mssql包不提供任何体面的选择。这个丑陋但完全一般的解决方案是将数据编码为十六进制测试,即

addslashes() isn't fully adequate, but PHP's mssql package doesn't provide any decent alternative. The ugly but fully general solution is encoding the data as a hex bytestring, i.e.

$unpacked = unpack('H*hex', $data);
mssql_query('
    INSERT INTO sometable (somecolumn)
    VALUES (0x' . $unpacked['hex'] . ')
');

摘要,那就是:

function mssql_escape($data) {
    if(is_numeric($data))
        return $data;
    $unpacked = unpack('H*hex', $data);
    return '0x' . $unpacked['hex'];
}

mssql_query('
    INSERT INTO sometable (somecolumn)
    VALUES (' . mssql_escape($somevalue) . ')
');

mysql_error()等价物是 mssql_get_last_message()

这篇关于如何使用PHP在SQL Server中转义字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:59