-1

我想在值中包含单引号的数据库中插入一个查询。我如何在 PHP 中处理这个问题?

我的查询是:

insert into query (date_time, userid, user_traits, query_sql, status, description, is_scheduled_row) 
values ('2016-01-06 02:39:01', '307', '0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687', 'Select * from gl_base_schema.item where national_status_cd = 'A'', 'in queue', ' (Scheduled Query #413) Pull all items where National Status Code is 'A'', 1);

它显示错误为

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'A'', 'in queue', ' (Scheduled Query #413) Pull all items where National Status C' at line 1
4

4 回答 4

1

将值中的单引号(')替换为反斜杠和单引号(\')两个单引号('')

尝试这个:

INSERT INTO QUERY (date_time, userid, user_traits, query_sql, STATUS, description, is_scheduled_row) 
VALUES ('2016-01-06 02:39:01', '307', '0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687', 'Select * from gl_base_schema.item where national_status_cd = ''A''', 'in queue', ' (Scheduled Query #413) Pull all items where National Status Code is ''A''', 1);

或者

INSERT INTO QUERY (date_time, userid, user_traits, query_sql, STATUS, description, is_scheduled_row) 
VALUES ('2016-01-06 02:39:01', '307', '0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687', 'Select * from gl_base_schema.item where national_status_cd = \'A\'', 'in queue', ' (Scheduled Query #413) Pull all items where National Status Code is \'A\'', 1);
于 2016-01-06T07:50:15.313 回答
0
$query = "Select * from gl_base_schema.item where national_status_cd = 'A'";
$sql = "insert into query (date_time, userid, user_traits, query_sql, status, description, is_scheduled_row) values ('2016-01-06 02:39:01', '307', '0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687', '."'".$query."'".', 'in queue', ' (Scheduled Query #413) Pull all items where National Status Code is \'A\'', 1)";
于 2016-01-06T07:55:48.827 回答
0

您可以像这样使用引号和单引号组合:

insert into query (date_time, userid, user_traits, query_sql, status, description, is_scheduled_row) 
VALUES ("2016-01-06 02:39:01","307","0,3598,1937,13891,37746,22082,2596,2431,12850,3917,1234784,44712,14638,14418,12850,2631,25003,11428,27450,2592,23593,11441,2826,36330,32219,32351,20720,13997,2594,2467,15687","Select * from gl_base_schema.item where national_status_cd = 'A'","in queue"," (Scheduled Query #413) Pull all items where National Status Code is 'A'", 1)

您可以'A'将此字符串用于另一个字符串,而不是将其用作"'A' test"

于 2016-01-06T08:02:10.930 回答
-1

你有的地方'A'',就去做'A'''。额外的'转义下一个',所以你需要''一个'。希望有帮助。

于 2016-01-06T07:52:49.783 回答