如何使用单引号将查询插入mysql
Posted
技术标签:
【中文标题】如何使用单引号将查询插入mysql【英文标题】:How to insert query into mysql with single quotes 【发布时间】:2016-01-06 07:46:22 【问题描述】:我想在值中包含单引号的数据库中插入一个查询。我如何在 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
【问题讨论】:
使用addlashes() php函数。 【参考方案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);
【讨论】:
【参考方案2】:$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)";
【讨论】:
【参考方案3】:你可以像这样使用 double 和 single quote 组合:
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"
【讨论】:
【参考方案4】:如果您有 'A''
,则改为 'A'''
。额外的'
会转义下一个'
,因此您需要''
才能获得一个'
。希望对您有所帮助。
【讨论】:
以上是关于如何使用单引号将查询插入mysql的主要内容,如果未能解决你的问题,请参考以下文章