explode() function using GROUP SEPARATOR

情到浓时终转凉″ 提交于 2019-12-02 02:40:22

问题


As per find through MySQL GROUP_CONCAT escaping this I have use GROUP_CONCAT

SELECT
  `topic_response`.`response`,
  GROUP_CONCAT(`comment` SEPARATOR 0x1D ) AS `comment`,
  `topic_response`.`add_date`
FROM `topic_response`
WHERE (topic_id = 286)
    AND (`comment` IS NOT NULL)
GROUP BY `response`
ORDER BY `add_date` desc

Then my output is properly separated but I don't know how to explode() it.

explode("0x1D", $comment) or     explode("\0x1D", $comment)

This does not work.


回答1:


If you still want to use 0x1D as seprartor, you can use following explode syntax:

explode("\x1D", $comment)



回答2:


Why are you using 0x1D as your seperator?

A way more convenient solution would to be to change GROUP_CONCAT('comment' SEPARATOR 0x1D ) to GROUP_CONCAT('comment' SEPARATOR "|" ) and then just do explode("|", $comment);.

If you expect "|" in your 'comments' you can replace the "|" with any string. So "asd42gfqwasdff33" would also be a valid seperator, which is quite unlikely to show up as a character in your comments.

But the real question is: why are you group concatting your comments and then exploding the results while you could just loop through the sql results (leaving the group_concat out).




回答3:


I have use chr function for explode.Basically 0x1D is ascii string 29

explode(chr(29), $comment)

Without no any doubts it solves in all condition.



来源:https://stackoverflow.com/questions/16476506/explode-function-using-group-separator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!