如何使用 PHP 在 SQL Server 中转义字符串? [英] How to escape strings in SQL Server using PHP?
本文介绍了如何使用 PHP 在 SQL Server 中转义字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为 SQL Server 寻找 mysql_real_escape_string()
的替代方案.addslashes()
是我最好的选择还是可以使用其他替代函数?
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()
的替代方法也很有用.
An 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 中转义字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文