-1

这是mysql查询

SELECT course_id
FROM course_master
WHERE year_id = '6'
AND course_name = ''What is Test?': Perspectives'

当我运行查询时,由于特殊字符问题,它会引发语法错误。我正在使用 mysql_escape_string 转义,但它不起作用。如何转义这些字符

4

3 回答 3

1

我正在使用 mysql_escape_string 进行转义,但它不起作用。

你用错了。您只需格式化字符串值,而不是整个查询

// here are your variables
$year = 6;
$name = "'What is Test?': Perspectives";

// let's format them
$year = intval($year);
$name = mysql_real_escape_string($name);

// and then insert in a query
$sql  = "SELECT course_id FROM course_master 
         WHERE year_id = $year AND course_name = '$name'";
于 2013-02-14T13:14:09.097 回答
0

当执行查询并且值有单引号时,将单引号加倍,这样就不会出错。

SELECT course_id
FROM course_master
WHERE year_id = '6'
AND course_name = '''What is Test?'': Perspectives'

但是如果你是在 PHP 上做的,那就用PreparedStatement那个。这也将防止 SQL Injection. 下面的文章将告诉你如何使用PHP Extensions.

于 2013-02-14T13:09:48.713 回答
0

试试这个查询 -

SELECT course_id
FROM course_master
WHERE year_id = 6
AND course_name = '''What is Test?'': Perspectives'

或使用转义 -

SELECT course_id
FROM course_master
WHERE year_id = 6
AND course_name = '\'What is Test?\': Perspectives'
于 2013-02-14T13:10:25.093 回答