如果我在MySQL Workbench或mysql终端中运行以下查询,它的工作正常:
SELECT
`P`.`human_name` AS `permissionName`,
IF(`PD`.`descriptor_text` REGEXP '.*role\\((-4,)?-3.*',
(SELECT group_concat(`id`) FROM `users`),
IF(`PD`.`descriptor_text` REGEXP '.*user\\([0-9,]+)$',
SUBSTR(
`PD`.`descriptor_text`,
INSTR(`PD`.`descriptor_text`, 'user(') + 5,
LENGTH(`PD`.`descriptor_text`) - INSTR(`PD`.`descriptor_text`, 'user(') - 5
),
NULL
)
) AS `user_ids`
FROM
`documents` AS `D`
INNER JOIN `permission_lookups` AS `PL` ON `D`.`permission_lookup_id` = `PL`.`id`
INNER JOIN `permission_lookup_assignments` AS `PLA` ON `PL`.`id` = `PLA`.`permission_lookup_id`
INNER JOIN `permission_descriptors` AS `PD` ON `PLA`.`permission_descriptor_id` = `PD`.`id`
LEFT JOIN `permission_descriptor_roles` AS `PDR` ON `PDR`.`descriptor_id` = `PD`.`id`
LEFT JOIN `permissions` AS `P` ON `PLA`.`permission_id` = `P`.`id`
WHERE `D`.`id` = 74
但是只要我把它放到php mysql_query()
中它就会失败并显示错误:
Got error 'parentheses not balanced' from regexp
我的PHP代码如下:
$permSQL = "
SELECT
`P`.`human_name` AS `permissionName`,
IF(`PD`.`descriptor_text` REGEXP '.*role\\((-4,)?-3.*',
(SELECT group_concat(`id`) FROM `users`),
IF(`PD`.`descriptor_text` REGEXP '.*user\\([0-9,]+)$',
SUBSTR(
`PD`.`descriptor_text`,
INSTR(`PD`.`descriptor_text`, 'user(') + 5,
LENGTH(`PD`.`descriptor_text`) - INSTR(`PD`.`descriptor_text`, 'user(') - 5
),
NULL
)
) AS `user_ids`
FROM
`documents` AS `D`
INNER JOIN `permission_lookups` AS `PL` ON `D`.`permission_lookup_id` = `PL`.`id`
INNER JOIN `permission_lookup_assignments` AS `PLA` ON `PL`.`id` = `PLA`.`permission_lookup_id`
INNER JOIN `permission_descriptors` AS `PD` ON `PLA`.`permission_descriptor_id` = `PD`.`id`
LEFT JOIN `permission_descriptor_roles` AS `PDR` ON `PDR`.`descriptor_id` = `PD`.`id`
LEFT JOIN `permissions` AS `P` ON `PLA`.`permission_id` = `P`.`id`
WHERE `D`.`id` = ".$argv[1];
$permQry = mysql_query($permSQL) or die('MySQL Error: ' . mysql_error());
while ($row = mysql_fetch_object($permSQL)) {
echo $row->user_ids;
}
什么会导致查询与PHP以外的任何其他工作正常?
答案 0 :(得分:2)
这是因为PHP将\\
理解为转义序列,将其转换为单个反斜杠\
。要打印两个文字反斜杠,您需要四个:
$permSQL = "
SELECT
`P`.`human_name` AS `permissionName`,
IF(`PD`.`descriptor_text` REGEXP '.*role\\\\((-4,)?-3.*',
(SELECT group_concat(`id`) FROM `users`),
IF(`PD`.`descriptor_text` REGEXP '.*user\\\\([0-9,]+)$',