1

I want to get some data via php from my SQL database:

GROUP_CONCAT(DISTINCT cat.name SEPARATOR ",") AS cats

this gives me a list:

sam, john, joe

I get some other data like this:

GROUP_CONCAT(DISTINCT cat.id SEPARATOR ",") AS cat_ids

here I get the result

1,2,3

Now here is my problem. I want to combine these values to get a result like this:

<a href="1">sam</a><a href="2">john</a><a href="3">joe</a>

My approach:

GROUP_CONCAT(CONCAT("<a href=\'",cat.id,"\'>",cat.name,"</a>")) AS cats 

But here I only get a blank white page as a result.

1
  • I found out is working, the blank white page was caused by another reason Commented Sep 26, 2017 at 13:35

2 Answers 2

5

I just used single quotes for string and double quote for attribute html. Moreover I used a space (" ") as separator in GROUP_CONCAT

CREATE TABLE GC1 (name VARCHAR(20), id INT);

    INSERT INTO GC1 VALUES ('sam',1);
    INSERT INTO GC1 VALUES ('john',2);
    INSERT INTO GC1 VALUES ('joe',3);
    SELECT * FROM GC1;
    SELECT GROUP_CONCAT( CONCAT('<a href="',id,'">',name,'</a>') SEPARATOR " ") AS X FROM GC1;

Output:

<a href="1">sam</a> <a href="2">john</a> <a href="3">joe</a>
Sign up to request clarification or add additional context in comments.

Comments

1

This

GROUP_CONCAT(CONCAT('<a href = "', `cat`.`id`, '">', `cat`.`name`, '</a>') SEPARATOR '') AS `cats`

worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.