在 IN 子句中使用 mysql_real_escape_string
mysql_real_escape_string 向 IN 子句中的值添加斜杠,因此不返回任何值。如何发送在 IN 子句中使用 mysql_real_escape_string() 转义的数组值?
这是我的代码:
$names_array = array('dave','smith');
$names = mysql_real_escape_string("'". implode("', '", $names_array) ."'");
$sql = "SELECT * FROM user WHERE user_name IN ($names)";
$results = mysql_query($sql);
mysql_real_escape_string 更改后的查询如下:
SELECT * FROM user WHERE user_name IN (\'dave\', \'smith\')
我不想在 IN 子句中使用这些斜杠。另外,我不希望直接在 IN 子句中替换值。 提前致谢。
mysql_real_escape_string adds slashes to the values in IN clause and hence no values are returned. How can I send array values that are escaped using mysql_real_escape_string() in IN clause?
Here is my code:
$names_array = array('dave','smith');
$names = mysql_real_escape_string("'". implode("', '", $names_array) ."'");
$sql = "SELECT * FROM user WHERE user_name IN ($names)";
$results = mysql_query($sql);
Query after mysql_real_escape_string changes like this:
SELECT * FROM user WHERE user_name IN (\'dave\', \'smith\')
I don't want these slashes here in IN clause. Also I don't want the values directly substituted in IN clause.
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这或许可以做到。
This might do it.
不要使用 mysql_real_escape_string ;根本不要直接使用
mysql_*
函数;使用 ADODB 或类似的东西;不要以这种方式连接查询,请使用占位符 (?
) 和准备好的语句。您的代码应类似于以下内容:Don't use
mysql_real_escape_string
; don't use themysql_*
functions directly at all; use ADODB or somesuch; don't concatenate your queries in this way, use placeholders (?
) and prepared statements. Your code should look similar to this: